> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baselayer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Idempotent Requests & Retries

> Retry any request that starts a search without creating, or paying for, a second one.

Baselayer accepts an `Idempotency-Key` header on the [endpoints that start a search or request](#supported-endpoints). If you retry a request with the same key, for example after a timeout or a `500`, Baselayer returns the resource the first request created instead of creating and billing a new one.

The header is optional. Requests without it work exactly as before.

***

## How It Works

Create a unique key for each search you mean to run, ideally a UUID. Save it with your own record before you send the request, and send the same key with every retry.

**1. Send the request with a key**

`POST /searches` with `Idempotency-Key: K`. Baselayer creates a search.

**2. The response is lost**

A timeout, a `500` or a restart means you never see the search ID.

**3. Retry with the same key and body**

Baselayer returns the same search, with the header `Idempotent-Replayed: true`, instead of creating a second one.

| Rule | Detail |
| - | - |
| Format | 1 to 255 printable characters. A UUID is ideal. |
| What must match | The method, path, query and JSON body. Key order and whitespace don't matter. Any other change does, including a different `reference_id` or an added or removed field. |
| Scope | Your application and the resource type. The same key on `/searches` and `/lien_searches` names two separate resources. `/lien_searches` and `/lien_searches/adhoc` share keys, and so do `/docket_searches` and `/docket_searches/adhoc`. |
| How long a key lasts | For the life of the resource. Keys don't expire. |
| Billing | Only the first request is billed. A replay is free. |

***

## Sending a Key

```bash theme={null}
curl -X POST https://api.baselayer.com/searches \
  -H "X-API-Key: your_api_key_here" \
  -H "Idempotency-Key: 4f1c2a7e-0b9d-4c3e-9a1f-2d6e8b7c5a10" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Acme Corp",
    "address": "123 Main St, San Francisco, CA 94105",
    "reference_id": "application-8812"
  }'
```

In Python, create the key once and store it, so every retry reuses it:

```python theme={null}
if not application.idempotency_key:
    application.idempotency_key = str(uuid.uuid4())
    application.save()

response = requests.post(
    "https://api.baselayer.com/searches",
    headers={"X-API-Key": api_key, "Idempotency-Key": application.idempotency_key},
    json={"name": application.business_name, "address": application.business_address},
    timeout=30,
)
replayed = response.headers.get("Idempotent-Replayed") == "true"
```

***

## What a Replay Returns

A replay returns the original resource, with the same `id`, `Location` and `X-Resource-Id`, plus the header `Idempotent-Replayed: true`. Header names can arrive in lowercase, so compare them case-insensitively.

The status code follows the resource's `state` when Baselayer responds:

| `state` | Status code |
| - | - |
| `PENDING` or `EXECUTING` | `202 Accepted` |
| `COMPLETED`, `FAILED` or `CANCELLED` | The endpoint's normal success code: `201 Created`, or `200 OK` for TIN verification |

A synchronous retry that finds the search still running waits for it, like a new synchronous request. If the wait runs out, you get `202`. See [Synchronous & Asynchronous Request Execution](https://docs.baselayer.com/docs/synchronous-asynchronous-request-execution.md).

A replay never re-runs a finished search. If the original ended in `FAILED`, the replay returns it with a success status code and `state: FAILED`, so always check `state`. To run a search again, for example after fixing the address, send it with a new key.

If the first request got a `500` after Baselayer saved it, a retry with the same key returns the saved search and makes sure it runs. Without a key, the retry creates a second search.

If two requests with the same key arrive at the same time, Baselayer creates one resource and the second request returns it as a replay.

***

## Errors

| Status and code | What it means | What to do |
| - | - | - |
| `422`, code `6700` | The key was already used with a different request. Nothing new was created. `metadata.resource_id` names the resource the key belongs to. | Send a new key for a new request. |
| `409`, code `6701` | Rare. A request with this key is still being saved. | Retry with the same key. |
| `422` with no code, on `Idempotency-Key` | The key is blank, longer than 255 characters, or contains non-printable characters. | Send 1 to 255 printable characters. |

***

## Finding a Resource by Its Key

Every resource created with a key carries it as `idempotency_key`, in the response body and in its webhooks. If you never received the resource ID, match the webhook to your record by `idempotency_key`. `Order.created` events are about billing and don't include it.

`reference_id` is different: it labels a resource with your own ID, and many searches can share one. Use both.

***

## Supported Endpoints

| Endpoint | Method | Description |
| - | - | - |
| `/searches` | POST | Business search |
| `/person_searches` | POST | Person search |
| `/international/searches` | POST | International business search |
| `/lien_searches` | POST | Liens search |
| `/lien_searches/adhoc` | POST | Ad hoc lien search |
| `/docket_searches` | POST | Litigation & bankruptcy search |
| `/docket_searches/adhoc` | POST | Ad hoc docket search |
| `/dockets/{docket_id}/details` | PUT | Order docket details |
| `/dockets/exhibits/{docket_exhibit_id}` | PUT | Order docket exhibit documents |
| `/dmf/searches` | POST | Death Master File search |
| `/tin_verifications` | POST | TIN verification |
| `/web_presence_requests` | POST | Web presence analysis |
| `/website_analysis_requests` | POST | Website analysis |
| `/naics_prediction_requests` | POST | Industry (NAICS) prediction |
| `/employee_verification_requests` | POST | Employment verification |

Any other endpoint ignores the header, so a retry there creates a new resource. That includes batch and export requests.

Know Your Agent credential mints require a key and keep it for 24 hours. See [Minting Credentials](https://docs.baselayer.com/docs/minting-credentials.md). MCP tools take the key as an `idempotency_key` argument. See [MCP Tools](https://docs.baselayer.com/docs/mcp-tools.md#retries-and-idempotency_key).

***

## Best Practices

* Save the key before you send the request. A key created inside the retry loop protects nothing.
* Retry timeouts, `5xx` responses and `409` code `6701` with the same key. For other `4xx` errors, fix the request and use a new key.
* Back off between retries, for example 1, 2, then 4 seconds.

***

## Next Steps

* [Handle sync and async responses](https://docs.baselayer.com/docs/synchronous-asynchronous-request-execution.md)
* [Set up webhooks](https://docs.baselayer.com/docs/webhooks-at-baselayer.md)
* [See the `Idempotency-Key` header on each endpoint in the API Reference](https://docs.baselayer.com/api-reference)

For questions about retries or integration support, contact your Baselayer account manager.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.