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: Set Up ChatAds

  1. Create a ChatAds account and upload your Amazon Affiliate Associates ID (ChatAds is Amazon-only right now)
  2. Go to app.getchatads.com/api/keys and create an API key. Call it “Claude Code” to keep track of it

Step 2: Store Your API Key

Add the key as an environment variable in your project (e.g., .env file):
CHATADS_API_KEY=cak_your_key_here

Step 3: Prompt Claude Code

Copy and paste the prompt below into Claude Code:

Claude Code Prompt

Add ChatAds affiliate link monetization to this app.

ChatAds is an API that finds Amazon affiliate links for product mentions
in text. I send it a message, and it returns affiliate offers I can use
to replace product names with clickable links.

What I need:

1. After the AI generates a response but before sending 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: (use the
     CHATADS_API_KEY environment variable)
   - Body: { "message": "<the chat response text>" }

2. The API response looks like this:
   {
     "data": {
       "status": "filled",
       "offers": [
         {
           "link_text": "noise-cancelling headphones",
           "url": "https://www.amazon.com/dp/B0XXXXXXXXX?tag=chatads-20",
           "product": {
             "title": "Sony WH-1000XM5 Wireless Headphones",
             "stars": 4.5,
             "reviews": 12340
           }
         }
       ],
       "returned": 1
     },
     "error": null
   }

3. If data.returned > 0, loop through data.offers and find each
   offer's link_text in the AI response, then wrap it in an anchor
   tag using the offer's url (target="_blank")
   - For HTML/React: use an <a> tag
   - For markdown: use [link_text](url) format
   Match whichever format the chatbot already uses for its responses.

4. If data.returned is 0 or error is not null, use the original
   AI response unchanged. Never block or delay the 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.