Skip to main content

Sign in, set roles and invite your team

What you will build​

You let the people of an account sign in to Black with an email and a password, act in an account and an environment of their choice, and do only what their role allows. You create roles, invite team members by email, and read every refusal a screen can meet.

The guide has two readers. If you build the screens of a console, read it from the top: each section says what to send and what each answer means for a screen. If you own an account, read Choose what each role may do, Create, change and delete a role and Invite, resend and revoke.

Before you start​

You need to know how your server calls Black: see Authentication. Your server's API client keeps its own access token, and a person's session is a separate thing: a person never uses an API client's token, and an API client never uses a person's session token.

An account holds two environments, live and test, each with its own data and API keys. A person belongs to an account's team, and the team belongs to the account, not to an environment: one membership reaches both environments, and the request chooses which one it acts in.

Black serves no page. The screens are yours: Black answers their requests and sends two emails that link to two pages of yours. See Give Black the origins and the two page addresses.

Steps​

  1. Get a first person onto a team. A Black operator adds the first person of an account, with the owner role, and gives you a single-use link to set a password. The link works once and for 60 minutes. From then on, an owner invites the others.

  2. Set the password. Send a POST request to /v1/console/password-resets with the email, the token of the link and the new password. It answers 204. A password has at least 12 characters, and a shorter one is refused with 422 and the code parameter_invalid, naming password. A wrong, used or expired token, or one that is not for that email, is refused with 422 and password_reset_invalid, naming token. Setting a password ends every session the person holds.

  3. Sign in. Send a POST request to /v1/console/sessions with the email and the password. It answers 201 with a session: its id, expires in Unix seconds, the team_member and teams, one entry for each account where the person is active, with the account's name, the vendor whose own account it is (or null) and the person's role there. It also holds the token. The answer carries Cache-Control: no-store, so keep it out of any cache.

  4. Send the token on each call. Send Authorization: Bearer <token>, and name the account and the environment in two headers, as the next section shows. The token lives 8 hours, with no refresh and no idle limit. When it ends, the next call answers 401 and the person signs in again.

  5. Read the session. Send a GET request to /v1/console/sessions/current with the token. It answers the same session without the token, so a screen can show who is signed in and where.

  6. Sign out. Send a DELETE request to /v1/console/sessions/current. It answers 204 and the token stops working.

Choose the account and the environment on each call​

A person's request names two headers.

HeaderValueMeans
Black-AccountAn account ID, acct_…The account to act in
Black-Modelive or testThe environment of that account

A request without Black-Mode, or with another value, is refused with 400 and the code mode_header_invalid. The check on this header comes first. A request without Black-Account is refused with 403 and account_required. The two headers choose among the accounts and environments the person's memberships reach, and nothing else.

curl https://api.black.example/v1/customers \
--header "Authorization: Bearer $SESSION_TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Black-Mode: test"

The account decides what the person reaches:

  • An account where the person is active: its environment, with the role of that membership.
  • A sub-account of a vendor, when the person is active on the vendor's own team: the sub-account's environment, with the person's role in the vendor's team. See Reach a vendor's sub-accounts.
  • Any other account, one the person once belonged to, one that does not exist, and a value that is no account ID: 403 with the code account_not_accessible, and the same body in every case, so the answer tells nothing about which accounts exist.

A person's calls use the same /v1 requests as your server, and the role decides which. Black reads the membership and its role on every request, so a role changed, or a membership turned off, holds from the next request.

Read each refusal​

Each refusal gives an error with a code. See Errors for every code. For a screen:

  • 401 authentication_required or access_token_invalid. The request holds no token, or a token that does not work: it has ended, the person signed out, or it is an API client's token on a request for people. Send the person to the sign-in screen.
  • 401 sign_in_failed. The email or the password is wrong. A wrong password, an email that is no one's and a missing field all answer the same body, each after about a second, so a screen cannot tell which emails exist, and neither can anyone else. Ask for both again.
  • 403 team_member_inactive. The password is right, but every membership of the person is turned off. Tell the person to ask their account's owner.
  • 403 permission_denied. The person's role lacks the permission the request needs. The error names the permission. Nothing was validated, read or changed. Hide or disable the action for that role. A person whose role holds team.read reads the role's permissions from GET /v1/roles; the five built-in roles all hold it.
  • 403 account_not_accessible. The person has no active membership of the account named, or one that was turned off. Show the person's own accounts from teams and let them choose.
  • 403 account_required. The request names no account, or a vendor's own account on a route that does not take it. Add the header, or name a sub-account.
  • 403 team_member_required. The route is for people only, and an API client sent it. Send it with a person's session token.
  • 403 owner_required. Only an owner may do it. Ask an owner of the account to do it. See Create, change and delete a role.
  • 429 rate_limited. After five sign-in attempts for one email within a minute that did not succeed, or 30 from one address within a minute, the next attempts are refused, the right password included. Read Retry-After, the seconds to wait, and ask the person to wait. A sign-in that succeeds clears the count of its email. Black counts a person's other calls as it counts an API client's, for each person, whichever account the request names.

Send an Idempotency-Key on writes as you do from a server. Black keeps each caller's keys apart: the same key sent by another person, or by an API client, is a request of its own, and that caller is never given your answer. See Idempotency.

Give Black the origins and the two page addresses​

Give Black this once for each deployment of Black that your screens call. Black holds it as configuration, so a Black operator sets it for you.

  • The origins. Give the origins your pages are served from, such as https://console.example.com, each as scheme, host and port, with no path. A browser from any other origin is not allowed. A request from an allowed origin may carry Authorization, Content-Type, Idempotency-Key, Black-Account, Black-Mode and Black-Version, and a page may read Retry-After, the three X-RateLimit- headers, Request-Id and Idempotent-Replayed. Black allows no cookie: the token travels in a header.
  • The page that sets a password. Black links to it from the first-time link and from the password-reset email, as <address>?token=…&email=…. The page sends the token, the email and the new password to /v1/console/password-resets.
  • The page that accepts an invitation. Black links to it from the invitation email, as <address>?token=…. The page follows Accept an invitation.

Until an address is set, the email holds the token alone and says that the address is not set, and never a broken link.

Choose what each role may do​

A permission is one thing a person may do. The list is fixed, and GET /v1/permissions answers it to any caller, in this order, each with its key and description. Every account has five roles, made with the account, and each holds some of the permissions.

PermissionWhat it lets a person doOwnerAdminDeveloperSupportRead-only
customers.readSee customers, their overviews, histories and active entitlements.YesYesYesYesYes
customers.writeCreate, change and delete customers.YesYesNoNoNo
customers.flagFlag a customer and remove the flag.YesYesNoYesNo
customers.blockBlock a customer and remove the block.YesYesNoYesNo
customers.exportExport the customer list, with every listed customer's name, email and phone.YesYesNoNoNo
customer_sessions.createMake a sign-in token that signs a customer in to the account's apps.YesYesNoNoNo
subscriptions.readSee subscriptions, invoices and plans.YesYesYesYesYes
subscriptions.createCreate subscriptions and change their metadata.YesYesNoNoNo
subscriptions.cancelCancel a subscription.YesYesNoYesNo
subscriptions.resumeUndo a subscription's cancel.YesYesNoYesNo
subscriptions.change_planChange a subscription's plan and undo a pending change.YesYesNoYesNo
subscriptions.grantGrant a subscription for free.YesYesNoYesNo
invoices.mark_paidReport an invoice paid, and report or settle a dispute of an invoice the account collected.YesYesNoYesNo
invoices.refundRefund an invoice's payment: through the gateway where Black charged it, or recorded where the account collected it.YesYesNoNoNo
catalog.readSee products, prices and features.YesYesYesYesYes
catalog.writeCreate, change and delete products, prices and features.YesYesYesNoNo
prices.move_subscribersMove the subscribers of one price to another.YesYesNoNoNo
settings.readSee the account's settings.YesYesYesNoYes
settings.writeChange the account's settings.YesYesNoNoNo
data_processing_agreement.readSee the data-processing agreement in the settings.YesYesNoNoNo
api_clients.manageCreate, see and revoke API clients.YesYesNoNoNo
apps.readSee the environment's websites and mobile apps.YesYesYesNoNo
apps.manageRegister an app, change its name and allowed origins, and archive it.YesYesNoNoNo
checkout.manageCreate and see checkout sessions.YesYesYesNoNo
webhooks.readSee webhook endpoints and their deliveries.YesYesYesNoYes
webhooks.manageCreate, change and delete webhook endpoints and roll their secrets.YesYesYesNoNo
gateways.manageConnect the payment gateway, see its connection, replace its credentials and disconnect it.YesYesNoNoNo
events.readSee the event log.YesYesYesYesYes
activity.readSee the activity log.YesYesYesYesYes
test_clocks.manageCreate, advance and delete test clocks.YesYesYesNoNo
team.readSee the team and its roles.YesYesYesYesYes
team.manageManage the team's members and roles.YesYesNoNoNo
vendor.accounts.readSee the vendor's client accounts.YesYesYesYesYes
vendor.accounts.createCreate client accounts for the vendor.YesYesNoNoNo

The list holds 34 permissions. Owner and Admin hold all of them. Developer holds 14: every .read permission except data_processing_agreement.read, and catalog.write, webhooks.manage, test_clocks.manage and checkout.manage. Support holds 14: it reads customers, subscriptions, the catalogue, events, the activity log and the team, and it flags and blocks customers, cancels, resumes and changes subscriptions, grants a subscription for free, reports an invoice paid and reads a vendor's sub-accounts. Read-only holds 9: every .read permission except data_processing_agreement.read and apps.read.

Only Owner and Admin manage the team and the API clients, export the customer list and see the data-processing agreement. A person whose role lacks data_processing_agreement.read gets the settings without that field. The two vendor.accounts permissions have an effect only in a vendor's own account.

Owner differs from Admin in two ways. Owner holds every permission that is added to the list later, and Admin holds the list as it was when the account was made. Only an owner touches the owner role, as the next section says.

An account made before apps.read, apps.manage, checkout.manage, customer_sessions.create or invoices.refund was added holds that permission in the Owner role only. Add it to the other roles of that account where you want it.

The permissions stay the same for every account. A role is made of them, and each account chooses its own roles.

Create, change and delete a role​

These requests need team.manage, and an API client is refused with 403 and team_member_required: a server's credential does not change who may do what. Send Black-Mode as for any call, though the roles and the team belong to the account and not to an environment.

  1. Read the roles. Send a GET request to /v1/roles, which needs team.read. The answer lists the account's roles, five at first. A role has an id that starts role_, a name, its permissions as keys in the list's order, owner and created.

  2. Create a role. Send a POST request to /v1/roles with a name of 1 to 100 characters, unique in the account, and permissions, a list of keys that may be empty. It answers 201 with the role.

  3. Change a role. Send a PATCH request to /v1/roles/{id} with a name, permissions or both. permissions is the whole new list. The change holds from each holder's next request.

  4. Delete a role. Send a DELETE request to /v1/roles/{id}. It answers 200 with deleted set to true.

curl --request POST https://api.black.example/v1/roles \
--header "Authorization: Bearer $SESSION_TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Black-Mode: live" \
--header "Content-Type: application/json" \
--data '{"name":"Refunds desk","permissions":["customers.read","subscriptions.cancel"]}'

A key that is not on the list is refused with 422 and parameter_invalid, naming permissions.0 for the first key, and a name the account already uses is refused with 422 naming name. Nothing changes. A key repeated in the list is kept once.

The rules for a role:

  • The owner role is fixed. It cannot be renamed, changed or deleted: 409 owner_role_fixed. It holds every permission on the list. No other role can be made an owner role.
  • A role in use cannot be deleted. A role that a membership holds, active or turned off, or that a pending invitation gives, is refused with 409 and role_in_use. Give the people another role, or revoke the invitation, then delete it.
  • Only an owner touches the owner role. Giving the owner role, taking it away, and turning off, turning on or removing an owner needs an owner: otherwise 403 and owner_required.
  • An account always keeps an active owner. Giving the last active owner another role, turning them off or removing them is refused with 409 and last_owner.
  • A role of another account does not exist. It answers 404 and resource_not_found.

Change a team member's role and access​

The team members are the people of the account, one membership each. Send a GET request to /v1/team-members to list them, with the filters status, active or inactive, and role, the ID of a role. A team member has an id that starts tm_, a person with id, name and email, a role, a status and created. The same person on two teams has two team members.

  • Give a role. Send a PATCH request to /v1/team-members/{id} with role, the ID of one of the account's roles.
  • Turn access off. Send a POST request to /v1/team-members/{id}/deactivate. The person's next request to this account is refused with account_not_accessible. When it was their last active membership, every session they hold ends.
  • Turn access on. Send a POST request to /v1/team-members/{id}/reactivate. The person signs in again.
  • Remove a member. Send a DELETE request to /v1/team-members/{id}. The membership goes for good. The person and their other teams stay.

Turning off a member who is off is refused with 409 and team_member_already_inactive, and turning on a member who is on with 409 and team_member_already_active. A person may change, turn off or remove their own membership under the same rules.

Invite, resend and revoke​

An owner or an admin, or anyone whose role holds team.manage, invites a person by email with one of the account's roles. These requests are for people only.

  1. Invite a person. Send a POST request to /v1/invitations with the email and the role, the ID of one of the account's roles. It answers 201 with the invitation, and Black sends the email. The invitation has an id that starts invite_, the email, the role, a status, expires, created and invited_by. It never holds the token.

  2. List the invitations. Send a GET request to /v1/invitations, with the filter status: pending, accepted, revoked or expired.

  3. Send it again. Send a POST request to /v1/invitations/{id}/resend. The invitation gets a new token and 7 new days, and a new email goes out. The old link stops working.

  4. Revoke it. Send a POST request to /v1/invitations/{id}/revoke. The link stops working.

curl --request POST https://api.black.example/v1/invitations \
--header "Authorization: Bearer $SESSION_TOKEN" \
--header "Black-Account: $ACCOUNT" \
--header "Black-Mode: live" \
--header "Content-Type: application/json" \
--data '{"email":"person@example.com","role":"'"$ROLE"'"}'

An invitation is good for 7 days, and an email has at most one open invitation to an account. The refusals:

  • 409 already_team_member. The email is on the account's team, active or turned off.
  • 409 invitation_pending. The email has a pending invitation. Resend it instead.
  • 409 invitation_not_pending. You resend or revoke an invitation that is accepted, revoked or expired.
  • 403 owner_required. You invite with the owner role and you are not an owner.
  • 404 resource_not_found, naming role. The role belongs to another account.

An expired invitation stays expired and a new invitation for its email replaces it.

Accept an invitation​

The page that accepts an invitation takes the token from the link and calls two requests that need no session. Each answers at most 30 requests a minute from one address.

  1. Read the invitation. Send a GET request to /v1/console/invitations/{token}. For a pending invitation it answers the account_name, the email, the role_name and existing_person. When existing_person is false, the page asks for a name and a new password. When it is true, the person already has a Black password, and the page asks for it.

  2. Accept it. Send a POST request to /v1/console/invitations/{token}/accept. A new person sends a name and a password of at least 12 characters. A person who exists sends their current password. It answers 201 with a session, as a sign-in does, so the person is signed in.

An unknown, expired, revoked or already accepted token is refused with 404 and invitation_invalid, the same on both requests. A wrong password for a person who exists is refused with 401 and sign_in_failed, and the invitation stays pending. It counts in the limit of five attempts for the email, as a sign-in does. A person who joined the team meanwhile is refused with 409 and already_team_member.

Reset a forgotten password​

A person who forgot the password asks for a link. Send a POST request to /v1/console/password-resets/request with the email. It always answers 202 with the same body, whether the email is a person's or not, and always after the same time, so it tells nothing about who has an account. For a person's email, Black sends a link to the page that sets a password, good for 60 minutes and given once a minute for each person. The page then sets the password as in step 2 of Steps.

Reach a vendor's sub-accounts​

A vendor's staff are members of the vendor's own account, and a vendor's API clients are clients of the same account. A vendor's staff act in a sub-account by naming it in Black-Account, and they act with their role in the vendor's team. When the person also has a membership of the sub-account, that membership comes first. The sub-account's own people reach only their own account.

In the vendor's own account, the staff reach the activity log, GET /v1/permissions, the team routes and the vendor routes. Any other route answers 403 and account_required. A vendor route needs Black-Account to name the vendor's own account, and anything else answers 403 and vendor_client_required. See Create and act for sub-accounts.

Whatever a person does is recorded under their own ID, with the actor team_member, or vendor_team_member for a vendor's staff acting in a sub-account. No name or email is written into an entry. Read the entries with GET /v1/activity-entries, filtered by actor_type and actor. See Flag a customer and read the activity log.

A runnable sample​

Signing in needs a person, a person needs a first password from an operator or an invitation, and the link of an invitation arrives by email. A sample cannot read an email, so the sample below runs the requests that need no person, with your API client's access token, and the sections above show the rest as requests. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes and needs grep.

It reads the list of permissions and stops unless the list holds 34. It sends a role request with an API client's token, which is refused because the roles belong to people. It signs in with an email that no one holds, which is refused. It reads the current session without a token, which is refused. Its email ends in a number made from the time.

TOKEN=PLACEHOLDER_ACCESS_TOKEN
RUN=$(date +%s)$RANDOM

PERMISSIONS=$(curl --silent https://api.black.example/v1/permissions \
--header "Authorization: Bearer $TOKEN")
COUNT=$(echo "$PERMISSIONS" | grep -o '"object":"permission"' | wc -l | tr -d ' ')

if [[ "$COUNT" != "34" ]]; then
echo "The list holds $COUNT permissions, not 34."
exit 1
fi

curl --silent https://api.black.example/v1/roles \
--header "Authorization: Bearer $TOKEN" \
--header "Black-Mode: test"

curl --silent --request POST https://api.black.example/v1/console/sessions \
--header "Content-Type: application/json" \
--data "{\"email\":\"nobody-$RUN@example.com\",\"password\":\"a-wrong-password-$RUN\"}"

curl --silent https://api.black.example/v1/console/sessions/current

The first request answers 200 with 34 permissions, each with its key and description. The second answers 403 with the code team_member_required. The third answers 401 with sign_in_failed, after about a second, and the fourth answers 401. The sign-in of a real person, the two headers, the roles and team members, the invitations and the reset request are not in the sample for the reason above: run them with the requests in the sections above, with a session token in SESSION_TOKEN, and the ID of the account in ACCOUNT.

What to do next​