← Guides

Pause ads before a product runs out of stock

Have an AI assistant write a daily check that warns in Slack when ads run on a product with few days of stock left, and can pause or lower bids when you ask it to.

Ads on a product you cannot ship waste money. A long stock-out can also hurt the listing's ranking.

This script checks every advertised product once a day. It works out days of cover: units on hand divided by average daily units sold. If ads are running on a product with fewer than N days left, it posts a warning in Slack. Only when you run it with --apply and a read-write key does it pause those campaigns or lower their bids.

Claude, ChatGPT or Cursor writes the script from the prompt below.

What you need

  • A Build account at build.sellermate.ai with your Seller Central account connected in the console.
  • A Slack channel with an incoming webhook. A webhook is a private web address that lets a script post into a channel.
  • An AI assistant: Claude, ChatGPT or Cursor.
  • A place to run it daily: a small server, a GitHub Action or a Vercel cron job.

Steps

  1. Create a read-only API key. In the Build console, go to API keys and create a key with read access. Keys look like sm_live_.... It is shown once, so copy it now.
  2. Store the secrets. Save the key as BUILD_API_KEY and the Slack address as SLACK_WEBHOOK_URL. Keep both out of the script.
  3. Pick N, the warning threshold in days. Start with 14, or your supplier lead time plus a week if that is longer.
  4. Paste the prompt below into your assistant.
  5. Read what it builds. It should join inventory, units sold and ad spend by product.
  6. Run it in dry-run mode and check the numbers.
  7. Schedule it.

The prompt

Read the API docs at https://www.sellermate.ai/api-docs first. Use them
for exact field names and for the tool that updates campaigns.

Write a Python 3 script that warns in Slack when ads are running on a
product that is close to running out of stock.

Rules:
- Base URL is https://api.sellermate.ai/public/v1. Every tool is
  POST /tools/{name} with a JSON body and an X-API-Key header.
- Read the key from BUILD_API_KEY. Never hard-code or print it.
- Call get_user_context first and use the accounts it returns.
- Call get_inventory for units on hand.
- Call get_retail_product_performance for the last 30 full days.
  Average daily units sold = units sold / 30.
- Days of cover = units on hand / average daily units sold. List
  products with no sales separately.
- Call get_advertised_product_performance for the last 7 days to find
  products with ad spend, and get_campaign_performance
  (amazon_account_id, start_date and end_date are required) to find
  their campaigns.
- Flag advertised products with fewer than N days of cover. N defaults
  to 14 and is set with --days.
- Paginate while pagination.hasMore is true.
- On a 429, wait for Retry-After and retry.
- Post one Slack message per account to SLACK_WEBHOOK_URL. Show each
  flagged product's units on hand, daily rate, days of cover, 7-day ad
  spend and campaigns.
- Add --dry-run to print the message instead of posting.
- Add --apply. Without it the script must never write. With it, pause
  the flagged campaigns, or lower bids by --bid-cut percent. Preview
  every change, send an idempotency key with each write, and skip any
  campaign in an exclude list file.
- Use requests only. Include a README with the cron line.

Check it

Run python stock_guard.py --dry-run. Pick one flagged product. Take its units on hand from Seller Central, divide by the 30-day average, and compare. If it differs, the sales window is the usual cause. Ask the assistant which dates it used.

Errors to know:

  • 401: the key is wrong or revoked.
  • 403: the key cannot see that account.
  • 422: a guardrail blocked a write. Nothing reached the marketplace.
  • 429: too many requests. The limit is 60 a minute on Free and Build, 300 on Scale. The script should wait and retry.

Turn on changes, carefully

Run the alert for a week first, until the warnings are ones you would act on. Then create a read-write key limited to one account. Run --apply by hand and read the preview. Writes go through guardrails: budget caps, preview before apply, idempotency keys and an audit log. Check the audit log after the first run.

Keep the daily job on the read-only key. Apply by hand.

Run it every day

Remove --dry-run, run it once, and confirm the message lands in Slack. For 7:00 every day the cron line is 0 7 * * *. Check the README for the time zone.

Watch out for

  • Inbound shipments. Stock on its way is not on hand. If a shipment lands tomorrow, today's warning is noise. Ask the assistant to count inbound units, or check them before you apply anything.
  • Vendor Central accounts have different inventory. This guide uses seller inventory. For Vendor Central, have the assistant use the vendor inventory tool from the docs.
  • Do not pause brand-defence campaigns blindly. They protect your own brand name. Put them on the exclude list and decide by hand.
  • The script does not turn campaigns back on when stock returns. Ask for a resume step, or keep a list.

Related use cases

Stock-out guard: pause ads before a product runs out

Pause ads on a product when its stock is about to run out, so you do not pay for clicks you cannot fill.

Clauden8nBrands · AI builders

Try it on your own accounts.

Start free with one account.

Start freeMore guides