Create and act for sub-accounts
What you will build
You create a sub-account for a business you serve, create a customer inside it with your own API client, and create the sub-account's own API credentials. You integrate once, and your requests reach each of your sub-accounts.
Before you start
You need a vendor and a client ID and secret for it. You receive your client ID and secret from Black when your vendor is set up, for test and for live, and Black shows each secret once. Keep the secret on your server. Never put it in browser or mobile code.
A client belongs to one environment. A test client acts in the test environment of an account, and a live client in the live environment. Start with a test client.
You need an access token. See Authentication.
Steps
-
Exchange your client ID and secret for an access token. The response holds the access token:
curl --request POST https://api.black.example/v1/oauth/token \--data grant_type=client_credentials \--data client_id=PLACEHOLDER_VENDOR_CLIENT_ID \--data client_secret=PLACEHOLDER_VENDOR_CLIENT_SECRET -
Create a sub-account. Send a
POSTrequest to/v1/vendor/accountswith anameand acontact_email. The response holds the sub-account, and itsidstarts withacct_. The sub-account has its own live and test environments. -
Add an
external_referenceto the request when you keep your own ID for the business. You can later list your sub-accounts withGET /v1/vendor/accounts?external_reference=…. The same endpoint filters bycreated_fromandcreated_until.GET /v1/vendor/accounts/{id}retrieves one. -
Send the request again with the same
external_reference. Black answers200with the sub-account it created the first time, unchanged, and creates nothing. It ignores any other field of the repeated request. Use this to retry a create safely. -
Act for the sub-account. Send your own access token with the header
Black-Account: acct_…on any ordinary endpoint, such asPOST /v1/customers. The request runs as that sub-account, in the environment of your client's mode. The customer you create belongs to the sub-account, and its own API clients see it. -
Create the sub-account's own API client. Send
POST /v1/api-clientswithBlack-Accountand aname. The response holds the client'sidand itssecret. Black shows the secret once. Send anIdempotency-Keywith the request: a repeat with the same key answers without the secret, so store the secret from the first answer. Give the client ID and the secret to the business. See Idempotency.
Black records what you do for a sub-account with your client as the actor, so the sub-account's own clients can tell it from their own work.
Handle the refusals
A request answers 403 with the code account_required when you send your token to an ordinary endpoint without Black-Account. Your client belongs to a vendor, and a vendor's client reaches an ordinary endpoint only for a sub-account. Add the header.
A request answers 403 with the code account_not_accessible when Black-Account names an account that is not one of your sub-accounts. The same answer covers an account that does not exist, your own account and a value that is not an account ID. Check the ID, and take it from the response that created the sub-account.
A request to a vendor endpoint such as /v1/vendor/accounts ignores Black-Account. An ordinary account's client that calls a vendor endpoint answers 403 with the code vendor_client_required.
GET /v1/vendor/accounts/{id} answers 404 with the code resource_not_found for an account that is not one of your sub-accounts.
A runnable sample
The sample creates a sub-account, creates a customer inside it, and creates the sub-account's own API client. It writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where your access token goes, and reads the sub-account's ID from the first response. It needs sed.
TOKEN=PLACEHOLDER_VENDOR_ACCESS_TOKEN
ACCOUNT=$(curl --silent --request POST https://api.black.example/v1/vendor/accounts \
--header "Authorization: Bearer $TOKEN" \
--data name="Desert Tours" \
--data contact_email=owner@desert-tours.example \
--data external_reference=desert-tours-1042 \
| sed -n 's/.*"id":"\(acct_[0-9a-z]*\)".*/\1/p')
curl --silent --request POST https://api.black.example/v1/customers \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--data name=Noor \
--data email=noor@example.com
curl --silent --request POST https://api.black.example/v1/api-clients \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Idempotency-Key: desert-tours-first-client" \
--data name="Desert Tours server"
The first request answers 201. If you run the sample again, it answers 200 with the same sub-account, because the external_reference is the same.
What to do next
- Read how a client gets and sends its token in Authentication.
- Read how to retry a write safely in Idempotency.