Skip to main content

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​

  1. Register a web app. Send a POST request to /v1/apps with a name, kind set to web and allowed_origins. The response is 201 and holds the app, with an ID that starts with app_. See Register an app.

  2. Register a mobile app instead. Send kind set to ios with a bundle_id, or android with a package_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 the Black-App header.

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.

  1. Write each origin in full, such as https://shop.example.com. Black keeps it in lower case and drops the default port, so HTTPS://Shop.Example.com:443 is kept as https://shop.example.com. Two entries that are the same origin once kept are refused.

  2. Use https in a live environment. A test environment also takes http, but only for localhost and 127.0.0.1 with any port, so you can work on your own machine.

  3. Change the origins later. Send a PATCH request to /v1/apps/{id} with the full new list of allowed_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​

  1. Create the key. Send a POST request to /v1/apps/{id}/publishable-keys with no body. The response is 201 and holds the key in key. 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.

  1. Create the second key while the first is active.

  2. Deploy the pages or the app that carry the second key.

  3. Revoke the first key. Send a POST request to /v1/publishable-keys/{id}/revoke. The response holds the key with revoked set. See Revoke a publishable key. A request with the revoked key now answers 401 with publishable_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​

  1. Send Black-App with the bundle ID or package name the app was registered with, together with the key. Send no Origin header. A mobile key is refused with 403 and origin_not_allowed when Black-App differs, and when the request carries an Origin, 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.

  1. Expect the pre-flight to be answered. A browser sends OPTIONS with Access-Control-Request-Method and no key. Black answers 204 for any asking origin, naming the methods GET, POST, PATCH and DELETE and the headers Black-Publishable-Key, Authorization, Content-Type, Idempotency-Key, Black-Version and Black-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.

  2. Read the real request's answer. It carries Access-Control-Allow-Origin only 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 exposes Retry-After, the three X-RateLimit- headers, Request-Id and Idempotent-Replayed.

  3. Expect no Access-Control-Allow-Origin on 403 and origin_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​

  1. Read script_tag on the app. For a web app with an active key it holds an HTML <script> tag whose data-publishable-key attribute names the app's newest key that is not revoked. It is null for 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​

  1. Archive an app you no longer use. Send a POST request to /v1/apps/{id}/archive. The app is archived for good: its keys answer 401 with app_archived from 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​