Manage an app's keys and allowed origins
What you will build
You register an app for one website or mobile app, give it a publishable key, roll the key without a gap, and revoke the old one. You also set where the app may call Black from, and read each answer Black gives when a call comes from the wrong place.
Before you start
You need an API client whose role holds apps.manage. The owner and admin roles of an account made recently hold it: see Sign in, set roles and invite your team. Read Call Black from a customer's page or app first to see what a publishable key opens.
A vendor works on a sub-account's apps with the Black-Account header, as on every route.
Steps
Register the app
-
Register a web app. Send a
POSTrequest to/v1/appswith aname,kindset towebandallowed_origins. The response is201and holds the app, with an ID that starts withapp_. See Register an app. -
Register a mobile app instead. Send
kindset toioswith abundle_id, orandroidwith apackage_name. A mobile app has no allowed origins. Black keeps the bundle ID or package name exactly as you send it, and compares it with theBlack-Appheader.
Set the allowed origins
An allowed origin is a scheme, a host and an optional port, with no path, query, fragment, user name or wildcard. A web app holds 1 to 10.
-
Write each origin in full, such as
https://shop.example.com. Black keeps it in lower case and drops the default port, soHTTPS://Shop.Example.com:443is kept ashttps://shop.example.com. Two entries that are the same origin once kept are refused. -
Use
httpsin a live environment. A test environment also takeshttp, but only forlocalhostand127.0.0.1with any port, so you can work on your own machine. -
Change the origins later. Send a
PATCHrequest to/v1/apps/{id}with the full new list ofallowed_origins. The list replaces the old one. See Update an app.
A bad origin answers 422 with parameter_invalid, naming allowed_origins.<n> with the part that broke the rule.
Create a publishable key
- Create the key. Send a
POSTrequest to/v1/apps/{id}/publishable-keyswith no body. The response is201and holds the key inkey. See Create a publishable key.
A key starts with pk_test_ in a test environment and pk_live_ in a live one. It is an identifier, not a secret: Black shows it in full at any time with List an app's publishable keys, and it may sit in a page's source. The test and live environments each have their own apps and keys, so a test key never works on live data.
Roll a key without a gap
An app holds at most two active keys. A third answers 409 with publishable_key_limit_reached. Black never rolls a key for you.
-
Create the second key while the first is active.
-
Deploy the pages or the app that carry the second key.
-
Revoke the first key. Send a
POSTrequest to/v1/publishable-keys/{id}/revoke. The response holds the key withrevokedset. See Revoke a publishable key. A request with the revoked key now answers401withpublishable_key_revoked; the app's other key keeps working, and so do the sessions the app already made.
A second revoke of the same key answers 409 with publishable_key_already_revoked. Revoking is for good.
Call from a mobile app
- Send
Black-Appwith the bundle ID or package name the app was registered with, together with the key. Send noOriginheader. A mobile key is refused with403andorigin_not_allowedwhenBlack-Appdiffers, and when the request carries anOrigin, so a mobile key cannot be used from a page.
The same refusal answers a web key sent from an origin that is not allowed, or with no Origin. These checks identify a well-behaved caller and do not prove one: the session protects the customer.
Read the cross-origin answers
A browser asks before it sends a request from another origin.
-
Expect the pre-flight to be answered. A browser sends
OPTIONSwithAccess-Control-Request-Methodand no key. Black answers204for any asking origin, naming the methodsGET,POST,PATCHandDELETEand the headersBlack-Publishable-Key,Authorization,Content-Type,Idempotency-Key,Black-VersionandBlack-App, and lets the browser keep the answer for 600 seconds. The answer allows the pre-flight only: the real request is checked in full. -
Read the real request's answer. It carries
Access-Control-Allow-Originonly when its origin is one of the app's allowed origins, a refusal of the key included, so your page can read the error. It exposesRetry-After, the threeX-RateLimit-headers,Request-IdandIdempotent-Replayed. -
Expect no
Access-Control-Allow-Originon403andorigin_not_allowed. The browser then hides the answer from the page: look at the origin the page sends, not at the body.
Black sends no Access-Control-Allow-Credentials and sets no cookie on the Client API. The key and the session travel in headers, so a request from another site carries neither.
Read an app's script_tag
- Read
script_tagon the app. For a web app with an active key it holds an HTML<script>tag whosedata-publishable-keyattribute names the app's newest key that is not revoked. It isnullfor a mobile app, for an archived app and for an app with no active key. Every call in these guides is a direct HTTP call: none of them needs the tag.
Archive an app
- Archive an app you no longer use. Send a
POSTrequest to/v1/apps/{id}/archive. The app is archived for good: its keys answer401withapp_archivedfrom the next request, and it takes no new key.
A runnable sample
This sample registers an app and its key, creates the second key, shows the third refused, calls from an allowed and from another origin, answers a pre-flight, revokes the first key and shows it refused. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes, reads the IDs and the keys from the responses, and needs sed. Each write waits a second because a test environment takes five writes a second.
TOKEN=PLACEHOLDER_ACCESS_TOKEN
APP=$(curl --silent --request POST https://api.black.example/v1/apps \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"name":"Keys Demo","kind":"web","allowed_origins":["https://app.example.com"]}' \
| sed -n 's/.*"id":"\(app_[0-9a-z]*\)".*/\1/p')
sleep 1
FIRST=$(curl --silent --request POST "https://api.black.example/v1/apps/$APP/publishable-keys" \
--header "Authorization: Bearer $TOKEN")
FIRST_ID=$(echo "$FIRST" | sed -n 's/.*"id":"\(pkey_[0-9a-z]*\)".*/\1/p')
FIRST_KEY=$(echo "$FIRST" | sed -n 's/.*"\(pk_test_[0-9A-Za-z]*\)".*/\1/p')
sleep 1
curl --silent --request POST "https://api.black.example/v1/apps/$APP/publishable-keys" \
--header "Authorization: Bearer $TOKEN"
sleep 1
curl --silent --request POST "https://api.black.example/v1/apps/$APP/publishable-keys" \
--header "Authorization: Bearer $TOKEN"
sleep 1
curl --silent --request GET https://api.black.example/v1/client/config \
--header "Black-Publishable-Key: $FIRST_KEY" \
--header "Origin: https://app.example.com"
curl --silent --request GET https://api.black.example/v1/client/config \
--header "Black-Publishable-Key: $FIRST_KEY" \
--header "Origin: https://other.example.com"
curl --silent --request OPTIONS https://api.black.example/v1/client/config \
--header "Origin: https://app.example.com" \
--header "Access-Control-Request-Method: GET"
curl --silent --request POST "https://api.black.example/v1/publishable-keys/$FIRST_ID/revoke" \
--header "Authorization: Bearer $TOKEN"
sleep 1
curl --silent --request GET https://api.black.example/v1/client/config \
--header "Black-Publishable-Key: $FIRST_KEY" \
--header "Origin: https://app.example.com"
The app and the two keys answer 201. The third key answers 409 with publishable_key_limit_reached. The configuration read from the allowed origin answers 200, and from the other origin 403 with origin_not_allowed. The pre-flight answers 204. The revoke answers 200, and the revoked key then answers 401 with publishable_key_revoked.
What to do next
- Read each call's fields and answers in Register an app and Create a publishable key.
- Start a customer's session with the key in Start, refresh and end a customer session.
- Read what a key opens in Call Black from a customer's page or app.
- Read every error code in Errors.