All articles

ProductJuly 26, 2026· 7 min read

Automate Your Lead Exports With an API and Webhooks

Stop copying URLs into a dashboard every week. Here's how to build a nightly lead pipeline with an API, webhooks, idempotency keys and sensible error handling.

By Apollo Scraper Team

If you pull the same kinds of lead lists every week, doing it by hand gets old fast. Copy the URL, paste it, name the file, wait for the email, download, import, repeat. This post shows how to turn that loop into a small automated pipeline using an API and webhooks, with a working request example, the error handling that keeps it from breaking quietly, and ideas if you'd rather not write code at all.

You don't need to be a backend engineer to follow along. If you can read a curl command and you've used a tool like a spreadsheet script or a no-code automation platform, you'll be fine.

Why automate lead exports at all?

Manual exports are fine when you run one campaign a month. They start to hurt when:

  • You run many segments. An agency with twelve clients, each with three personas, has 36 searches to keep fresh.
  • Freshness matters. New hires and job changes are often the best prospects. A weekly or nightly pull catches them while the timing is still good.
  • Several tools are involved. The file has to go from the export into a cleaning step, then a CRM, then a sequencer. Every manual hand-off is a chance to forget something.
  • People go on holiday. If one person knows how the export works, the pipeline stops when they're away.

The goal isn't to remove humans from prospecting. It's to remove the boring copy-paste part so humans spend their time on the list strategy and the actual outreach.

The nightly pipeline, step by step

Here's a simple design that works well for most teams. Four stages, each doing one job.

  1. A list of search URLs. Keep a sheet or config file with one row per segment: the Apollo search URL, a file name pattern, how many leads to request, and whether to verify emails. This is the only part people edit by hand.
  2. A scheduled job that places orders. Every night (or every Monday), a script reads the list and sends one API order per row.
  3. A webhook receiver. When an order finishes, we POST to your callback URL. Your receiver notes the order ID and kicks off the next step.
  4. Download and import. The receiver downloads the CSV, runs your cleaning rules, removes existing contacts, and pushes the rest into your CRM with a source tag.

Notice that nothing in this design polls in a tight loop or waits around. Orders run on our servers in the background, and most are ready within 24 hours, often much sooner. The webhook tells you when to act.

Designing the search URL list

A good row in your config looks something like this: segment name uk-saas-revops, the full search URL copied from Apollo, contact_count 1000, verify_emails true. Put the date into the file name when you place the order, for example uk-saas-revops-2026-10-25, so every file is traceable later. Keep searches narrow; a pipeline makes it just as easy to automate a bad list as a good one.

Placing an order with the API

First, create a key on the API Key page in the dashboard. Store it as an environment variable or in your automation tool's secret store, never in the sheet itself. Anyone with the key can spend your credits. Our help article on getting your API key covers storage and regeneration.

Here's a People Scraper order with email verification and a webhook:

curl -X POST https://api.apolloscrapers.com/api/v1/apollo-people-scraper \
  -H "Authorization: Bearer $APOLLO_SCRAPER_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: uk-saas-revops-2026-10-25" \
  -d '{
    "search_url": "https://app.apollo.io/#/people?...",
    "results_file_name": "uk-saas-revops-2026-10-25",
    "contact_count": 1000,
    "verify_emails": true,
    "callback_url": "https://hooks.example.com/apollo-scraper"
  }'

The fields are short and predictable: search_url is the Apollo search, results_file_name is what the file will be called, contact_count is how many leads you want, verify_emails is optional and People-only, and callback_url is the https address we'll notify. Company and mobile orders use the same shape at /apollo-company-scraper and /apollo-mobile-scraper.

Before the nightly run, it's worth calling GET /me to check your balance. If you have 3,000 credits left and the night's orders need 13,000, it's better to send one alert than twelve failed orders.

Webhooks: knowing when the file is ready

When callback_url is set, we send a JSON POST to it as the order changes state, with events like order.processing, order.completed and order.cancelled. The request includes an X-ApolloScraper-Event header naming the event, plus a signature header so you can confirm it really came from us. The API documentation explains the signature check in detail.

A sensible receiver does very little in the request itself:

  1. Verify the signature. If it doesn't check out, reject it.
  2. Read the event type. Ignore anything you don't care about.
  3. For order.completed, put the order ID onto a queue or a "to process" row.
  4. Respond quickly with a 2xx status.

Then a separate step picks up the order ID, calls GET /orders/{id}/download to fetch the file, and handles cleaning and import. Keeping the receiver fast matters: if your handler takes a long time because it's importing 5,000 rows into a CRM, the delivery may time out and be retried, and now you're importing twice.

Tip: Don't rely on webhooks alone. Run a small daily check with GET /orders that looks for completed orders you haven't processed yet. Failed deliveries are retried a few times, but your receiver might be down for longer than that. A safety net costs ten lines of code.

Error handling and idempotency

Automations rarely fail loudly. They fail quietly, and you find out three weeks later when someone asks why the UK pipeline is empty. Plan for these cases.

Use an Idempotency-Key on every order

Networks drop requests. Your script times out, retries, and suddenly you've placed the same order twice and paid twice. Sending an Idempotency-Key header with a unique value per order stops that: a retried request with the same key won't create a duplicate. Build the key from things that identify the order, like segment name plus date. Then even if the whole job reruns, the same night's orders aren't placed again.

Make the webhook handler idempotent too

Webhooks can arrive more than once. Before processing, check whether you've already handled that order ID. If yes, return 2xx and do nothing.

Handle the common failures

What goes wrongWhat to do
Low balanceCheck GET /me before the run; alert and skip rather than fail order by order
Search URL rejectedLog the segment name, notify the owner, continue with the other rows
Network timeout on orderRetry with the same Idempotency-Key, with a short backoff
Order cancelledRecord it and flag the segment for a human to look at
Fewer leads than requestedNothing to fix: unused credits are refunded automatically, but log the count so you can widen the search
CRM import failsKeep the downloaded file and retry; files can be re-downloaded for 180 days

The one rule that matters most: one bad row should never stop the whole run. Wrap each order in its own error handling, log what happened, and keep going.

No-code options

You don't have to write a script. Most no-code automation platforms can do every step above:

  • Scheduled trigger: a "run every day at 2am" trigger reads rows from a spreadsheet.
  • HTTP request step: sends the POST with your headers and JSON body. Store the API key in the platform's secrets or connection settings.
  • Webhook trigger: most platforms give you a unique https URL you can paste in as callback_url.
  • CRM step: the platform's built-in CRM action creates or updates contacts from the downloaded rows.

The same advice applies: set an idempotency key, add a filter so only order.completed events continue, and send yourself a message when something fails. If you're mapping columns into a CRM for the first time, our post on importing Apollo CSVs into HubSpot, Salesforce and Pipedrive has a field mapping table you can copy.

Start small: automate one segment, watch it run for a week, then add the rest. Once it's steady, the only manual work left is deciding which searches deserve a spot on the list. When you're ready to build it, the API ordering guide and the API documentation have everything else you'll need.

100 free leads on sign up

Automate Your B2B Lead Generation With Apollo Scraper

Get started for free

No Credit Card Required*