Skip to main content

Overview

If you’ve built a chatbot or AI app using Claude Code, you can integrate ChatAds by giving Claude Code the prompt below. It will add a server-side call to the ChatAds API and insert affiliate links into your chat responses.
No SDK install needed for basic integration. Claude Code will use the REST API directly, or install the TypeScript or Python SDK if it fits your stack better.

Setup

Step 1: Get Your API Key

  1. Go to app.getchatads.com/api/keys
  2. Create an API key (starts with cak_)

Step 2: Prompt Claude Code

Copy and paste the prompt below into Claude Code. Replace cak_your_api_key with your actual API key.

Claude Code Prompt

Copy this entire prompt and paste it into Claude Code:
Add ChatAds affiliate link monetization to this chatbot.

**What ChatAds does:** I send it a text message (like a chat response) and it
returns affiliate links for any products mentioned in that text.

**What I need:**

1. After generating a chat response but before displaying it to the user,
   call the ChatAds API:
   - `POST https://api.getchatads.com/v1/chatads/messages`
   - Headers: `Content-Type: application/json` and `x-api-key: cak_your_api_key`
   - Body: `{ "message": "<the chat response text>" }`

2. The API response looks like this:
   ```json
   {
     "data": {
       "returned": 1,
       "offers": [{
         "link_text": "standing desk",
         "url": "https://amazon.com/...",
         "product": {
           "title": "Standing Desk, Adjustable Height...",
           "stars": 4.5,
           "reviews": 2340
         }
       }]
     }
   }
   ```

3. If `data.returned > 0`, find the first `link_text` in the chat response and
   replace it with a hyperlink using the `url`.
   - For HTML/React: use an `<a>` tag with `target="_blank"`
   - For markdown: use `[link_text](url)` format
   Match whichever format the chatbot already uses for its responses.

4. If `data.returned === 0` or there's an error, display the original
   response unchanged. Never block or delay the chat response if the
   ChatAds call fails.

Keep the API key server-side — don't expose it in client-side code or
environment variables prefixed with NEXT_PUBLIC_, VITE_, etc.

How It Works

The prompt adds a single step to your chatbot’s response pipeline:
  1. Your chatbot generates a response (via Claude, GPT, etc.)
  2. That response text is sent to the ChatAds API
  3. ChatAds identifies product mentions and returns affiliate links
  4. Matching text in the response is replaced with linked versions
  5. The final response (with or without links) is shown to the user

Troubleshooting

401 Unauthorized errors

Verify your API key is correct and being sent in the x-api-key header (not Authorization).
  1. Test your chat response text in the API Explorer to confirm it returns offers
  2. Not every message contains product mentions — ChatAds only returns links when it finds relevant products
  3. Check that the link_text replacement is case-sensitive and matching the exact text from the response

API key exposed in client-side code

If Claude Code put the API key in a file that gets shipped to the browser (e.g., a React component, a VITE_ or NEXT_PUBLIC_ env var), ask it:
Move the ChatAds API call to the server side. The API key should never
be in client-side code or public environment variables.

Response Reference

FieldTypeDescription
data.statusstring"filled" if offers found, "empty" otherwise
data.returnednumberNumber of offers returned
data.offers[].link_textstringText to match in the chat response
data.offers[].urlstringAffiliate link URL
data.offers[].product.titlestringProduct name
data.offers[].product.starsnumberProduct rating (0-5)
data.offers[].product.reviewsnumberReview count
See the full API Reference for all available parameters and response fields.

Best Practices

Keep keys server-side

The ChatAds API call should happen on the server. Never put your key in client-side code.

Don't block chat responses

If the ChatAds call fails or times out, show the original response. Never let monetization break the chat experience.

Handle empty gracefully

Not every message will have affiliate matches. Always check data.returned > 0 before modifying the response.

Test in API Explorer

Verify your messages return offers in the API Explorer before debugging your integration.