Skip to main content

Authentication

The systems of an account authenticate with a client ID and a client secret. Black gives you one pair for each environment of the account, live and test. You exchange the pair for an access token, then send the token with every request.

The ping needs no credentials. See Ping. Other addresses will need an access token.

Get your client ID and secret​

Black gives you a client ID and a client secret for each environment. Black shows the secret once, when it gives it to you, and nobody can read it again. Store it in a secret manager or an environment variable of your server.

Keep the secret on your server. Never put it in browser or mobile code.

Each pair belongs to one environment. An environment can hold two active pairs at once, so you can replace a secret without downtime: ask Black for a second pair, move your systems to it, then ask Black to revoke the first.

Exchange the pair for an access token​

Send a POST request to /v1/oauth/token with the form fields grant_type, client_id and client_secret. The grant type is always client_credentials:

POST /v1/oauth/token
grant_type=client_credentials&client_id=01a1cee6-32f9-7235-8cf1-59f356f5535f&client_secret=<your client secret>

The same request with cURL. The sample writes PLACEHOLDER_CLIENT_ID and PLACEHOLDER_CLIENT_SECRET where your own values go:

curl --request POST https://api.black.example/v1/oauth/token \
--data grant_type=client_credentials \
--data client_id=PLACEHOLDER_CLIENT_ID \
--data client_secret=PLACEHOLDER_CLIENT_SECRET

The response holds the access token:

HTTP/1.1 200 OK

{"token_type":"Bearer","expires_in":3600,"access_token":"<access token>"}

The body has three fields:

  • token_type: always Bearer.
  • expires_in: how long the token lasts, in seconds. A token lasts one hour.
  • access_token: the token to send with your requests.

Send the access token​

Send the token in the Authorization header of every request:

Authorization: Bearer <access token>

Put a credential in a header or in the request body, never in the address. A request whose query string carries access_token, client_secret, api_key or refresh_token is refused with 400 and credentials_in_query_string, before the credential is read, even when a valid header is sent beside it. The error names the parameter and never repeats its value. On the token address the same refusal answers invalid_request. An address can end up in a browser history, a proxy log or a shared link, which a header or a body does not.

The token works for one environment only. A token of the test environment reaches the test data of the account and nothing else. A token of the live environment reaches the live data and nothing else.

Get a new access token​

A token that has passed its lifetime is refused with access_token_invalid. Get a new one the same way: exchange the client ID and the secret again. Keep the token until shortly before it expires, and do not ask for a new token for each request: the token address has its own limits, below.

Fix a refused request​

A refused request to any address but the token address answers with the error shape of every other failure. See Errors. Fix each refusal by its code.

StatusCodeCause and fix
401authentication_requiredThe request has no Authorization header. Send Authorization: Bearer <access token>.
401authorization_header_malformedThe header is not the word Bearer, a space and an access token. Send it in that form.
401access_token_invalidThe token is unknown, not valid or past its lifetime. Get a new access token.
401api_client_revokedBlack revoked the client ID and secret the token was issued to. Use an active pair of the same environment, get a new token with it, and ask Black for a new pair if you have none.

Fix a refused token request​

The token address, POST /v1/oauth/token, is the one address that refuses in the body the OAuth 2.0 standard gives for a token endpoint (RFC 6749, section 5.2), so an OAuth client library reads the refusal as it reads any other. The body has two fields and no others:

  • error: the code of the refusal, one of the codes below. Branch on it in your own code.
  • error_description: a sentence that says what went wrong, for a developer to read.

A refusal also carries Cache-Control: no-store, Pragma: no-cache and the Request-Id header. See Request IDs.

StatusCodeCause and fix
401invalid_clientThe client ID or the secret is not valid. The description is the same whether the ID is unknown or the secret is wrong. Check both values for the environment you call.
401invalid_clientThe client ID and secret are right, but Black has revoked the pair. The description says that the client has been revoked. Use an active pair of the same environment, or ask Black for a new pair.
400invalid_requestThe request has no client_id or no client_secret. Send both.
400unsupported_grant_typeThe request has no grant_type, or one that Black does not issue tokens for. Send grant_type=client_credentials.
400unauthorized_clientThe grant_type is one your client ID may not use, such as authorization_code. Send grant_type=client_credentials.

When you send the client ID and secret in an Authorization: Basic header instead of the form fields, a 401 also carries WWW-Authenticate: Basic realm="OAuth".

A request over a limit of the token address is not a refusal of the standard: it answers 429 in the error shape of every other failure. See Stay within the rate limits.

A failed token request looks like this. This cURL request sends a secret that is not valid:

curl --request POST https://api.black.example/v1/oauth/token \
--data grant_type=client_credentials \
--data client_id=PLACEHOLDER_CLIENT_ID \
--data client_secret=not-the-client-secret
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Cache-Control: no-store, private
Pragma: no-cache
Request-Id: req_01m77ecgd8e4mzqea9tdav22hn

{"error":"invalid_client","error_description":"Client authentication failed"}

To replace a lost or exposed secret, ask Black to revoke the pair. A revoked pair stops working at once, and so do the tokens issued to it: their next request answers api_client_revoked.

Stay within the rate limits​

Black limits the requests made with the tokens of one client ID, per second. Reads and writes have a limit each, and the limits are the same in the test environment as in the live one:

Reads per secondWrites per second
10050

A GET request is a read. Any other method is a write.

Every answer to an authenticated request carries three headers:

  • X-RateLimit-Limit: the limit that applies to the request.
  • X-RateLimit-Remaining: how many more requests of that kind fit in the current second.
  • X-RateLimit-Reset: the time the count starts again, as a Unix timestamp in seconds.

A request over the limit answers 429 with the code rate_limit_exceeded, the same three headers, and a Retry-After header with the number of seconds to wait. Wait that long, then send the request again. Every 429 names the limit it refers to in its error, and carries Retry-After.

The token address has its own limits, per minute: 60 requests from one caller address, and 30 requests for one client ID, from any address. A request over either limit answers 429 with the code rate_limited and a Retry-After header, in the error shape of every other failure, not in the body of a token refusal. Wait, then send the request again.