# Polling and webhooks: how to get results back

> The three LocalPipe polling endpoints, the key they use, and why a polling loop must have no time limit. Use callback_url when your tool has a hard timeout.
>
> Part of the LocalPipe API reference: https://www.localpipe.io/docs/polling (full reference in one file: https://www.localpipe.io/localpipe-api-docs.md, categories CSV: https://www.localpipe.io/localpipe-business-categories.csv)

## Polling Endpoints

> **Note:** The polling endpoints use the **Supabase anon key** (via the `apikey` header), **not** your LocalPipe API key. The Supabase anon key is:
> `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InV1bnJsZmZvbm52bWJzc3RrcHplIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzA0OTIwMzMsImV4cCI6MjA4NjA2ODAzM30.8Ic_U17YrZtsDVnYnIP0XJzrhZJOLDSlJ0AZfTS31FQ`

| Endpoint | Purpose |
|----------|---------|
| GET /get-job-results?job_id=xxx | Poll enrichment results |
| GET /get-job-status?job_id=xxx | Poll enrichment status |
| GET /get-scrape-results?task_id=xxx | Poll maps search results |

> **These are GET requests with query-string parameters — not JSON bodies.** Sending `{"job_id": "..."}` in a POST body returns `400 Missing job_id parameter`. Likewise `/maps-search` expects `query` and `location` (both accept arrays); using `keywords`/`locations` returns `400 Missing required field: query`. A `400` is never retryable — read the `message` field, fix the parameter, resend.

### 🚨 REQUIRED: Use Unlimited Polling

**You MUST configure your polling loop with no maximum duration.** Stop polling **only** when the response contains your record/results — never when a timer expires.

LocalPipe operates a fair-share queue: during high-traffic periods, your records may sit in the pending queue for several minutes (occasionally longer) before processing begins. **Jobs never expire server-side.** If your client gives up early, you will:

- ❌ Never receive your results (even though we successfully processed them)
- ❌ Be charged credits for work you never see
- ❌ Mistakenly believe the API is broken when it's working as designed

**Correct polling pattern:**

```javascript
// ✅ CORRECT — polls forever until results arrive
async function waitForResults(jobId) {
  while (true) {
    const res = await fetch(`${API_BASE}/get-job-results?job_id=${jobId}`, {
      headers: { apikey: ANON_KEY }
    });
    const data = await res.json();
    if (data.status === 'completed') return data.records;
    if (data.status === 'failed') throw new Error(data.error_message);
    await new Promise(r => setTimeout(r, 5000)); // 5s between polls
  }
}
```

```javascript
// ❌ WRONG — gives up after 5 minutes, loses results
const TIMEOUT_MS = 5 * 60 * 1000;
const start = Date.now();
while (Date.now() - start < TIMEOUT_MS) { /* ... */ }
```

**Polling intervals:** every 5s for jobs ≤ 2,000 records, every 10s for larger jobs. The `/get-job-status` response includes a `recommended_poll_interval` field — respect it.

**For automation tools with hard HTTP timeouts (Clay, Zapier, Make):** Use `callback_url` instead of polling. We POST results to your webhook the moment they're ready, with no client-side waiting required.
