# Authentication

> Authenticate with your dg_ API key using Basic auth, a Bearer token or the X-API-Key header, and keep the key safe.

Every request carries one API key. Keys start with `dg_`, belong to a person, and bill that person's organization wallet.

## Ways to send the key

| Method | Header | Accepted on |
|---|---|---|
| Basic auth | `Authorization: Basic base64(any-login:dg_key)` | Compatible routes (`/v1/serp/...`, `/v1/pages/...`, `/v1/ai/...`, `/v1/business/...`, `/v1/ads/...`, `/v1/account/...`) |
| Bearer token | `Authorization: Bearer dg_...` | Every `/v1` route |
| API key header | `X-API-Key: dg_...` | Every `/v1` route |
| Query parameter | `?api_key=dg_...` | `/v1/business/google/products` only, for clients built for Scale SERP |

With Basic auth we ignore the login and authenticate the password as an API key. That's what lets a DataForSEO client switch over by changing only its password. The Crawl API routes (`/v1/crawls`, `/v1/map`, `POST /v1/serp`, `/v1/usage`) and `/v1/visibility/...` take Bearer or `X-API-Key`, not Basic.



```bash title="Basic"
curl "https://api.dataforgaio.com/v1/account/user_data" -u "api:$DATAFORGAIO_API_KEY"
```

```bash title="Bearer"
curl "https://api.dataforgaio.com/v1/account/user_data" \
  -H "Authorization: Bearer $DATAFORGAIO_API_KEY"
```

```bash title="X-API-Key"
curl "https://api.dataforgaio.com/v1/account/user_data" \
  -H "X-API-Key: $DATAFORGAIO_API_KEY"
```



## Authentication errors

Compatible routes answer in the DataForSEO envelope with a `WWW-Authenticate: Basic realm="api"` header:

| Code | HTTP | When |
|---|---|---|
| `40100` | 401 | No credentials were sent. |
| `40101` | 401 | The key is malformed, unknown or revoked, or the Basic password doesn't start with `dg_`. |
| `40101` | 403 | The key is valid but its owner is no longer an active member of an organization. |

Crawl API routes answer with our error body, for example:

```json
{ "status": "error", "error": { "http_status": 401, "code": "unauthorized", "message": "Missing, malformed, or revoked API key." } }
```

Neither costs anything.

## Keeping keys safe

- **Shown once.** The full key appears only in the response that creates it. We store a SHA-256 hash, so we can't show it to you again. Lost keys are replaced, not recovered.
- **Keep keys server-side.** Don't ship a key in a browser bundle, a mobile app or a public repository. Anyone holding it can spend your wallet.
- **Use environment variables or a secret manager.** The examples in these docs read `DATAFORGAIO_API_KEY`.
- **One key per system.** Separate keys for production, staging and each integration let you revoke one without touching the others. Rate limits are counted per key.
- **Revoke from the dashboard.** Deleting a key under **Dashboard → Developers** stops it on the next request.
- **Prefer headers to `?api_key=`.** The query parameter exists only for Scale SERP compatibility. Our request logs record the path, never the query string, but proxies and browser history on your side may keep it.
- **Set spending controls.** For automated callers, including AI agents, send `max_cost` on each task so a single request can't cost more than you expect. See [Tool-calling tips](https://dataforgaio.com/docs/tool-calling-tips.md).
