# API Authentication and Error Handling

Source: https://docs.premiumqualifiedleads.com/authentication/api-authentication-and-error-handling

Your account manager gives you the production base URL. Examples use `https://YOUR_API_HOST/api/v1`.

#### Which credential to use

| Who | Credential | How to send it |
|---|---|---|
| Supplier sending leads | Supplier API key | `x-api-key` header on `POST /ingest` and `GET /supplier/stats` |
| Buyer using the buyer API | Buyer API key, exchanged for a token | `Authorization: Bearer <token>` |
| Buyer's CRM calling back (rep, conversation, status updates) | Partner API key | `x-api-key` header on `/partners/:partnerKey/...` |
| Portal users | Email and password | Sign in to the portal; invites come from your account manager or your team admin |

#### Get a buyer token

```plaintext
POST /api/v1/auth/buyer
Content-Type: application/json

{ "api_key": "YOUR_BUYER_API_KEY" }

```

Response:

```json
{ "token": "YOUR_ACCESS_TOKEN", "partnerKey": "your_partner_key", "expiresIn": "24h" }

```

Tokens expire after 24 hours. Request a new one when you get a 401.

#### Status codes

| Status | Meaning | What to do |
|---|---|---|
| 400 | A required field is missing or invalid. | Fix the request. The body says which field. |
| 401 | Credential missing, invalid or expired. | Check the key or get a new token. |
| 403 | Valid credential, but not for this record. | You can only act on leads delivered to your account. |
| 404 | Not found, or the feature isn't on for your account. | Check the ID, or ask us to enable the feature. |
| 409 | Already done, for example an outcome already recorded. | Read the current status before retrying. |
| 422 | A business rule failed, for example the reporting window has closed. | Read the error message. |
| 429 | Too many requests. | Back off and retry. |
| 500 / 503 | Server-side problem. | Retry later. Contact us if it persists. |

#### Keeping credentials safe

- Store keys in server-side secrets, never in browser code, repositories or screenshots.
- Don't paste keys into support emails or chats.
- If a key may have been exposed, ask us to rotate it. Keys can be revoked immediately.
