Workspaces

API tokens

Create a personal API token for one workspace with the scopes it needs, use it on the routes that accept it, and revoke it.

A personal API token lets a script or CI job call the API without your password. A token belongs to you and to one workspace. It starts with phk_.

Dashboard or API

Each workspace has an API tokens page in the dashboard where you create, list and revoke your tokens. The same actions are available through the routes below, using a signed-in session.

Create a token

  1. Sign in with POST /auth/login and keep the token it returns. See "Accounts and sessions".
  2. Call POST /workspaces/{workspaceId}/api-tokens with that session token, a name of up to 100 characters, the scopes the token needs and optionally expiresInDays.
  3. Copy the token from the response now. It is shown once and cannot be read again.
Create a token
curl -sS -X POST https://api.perkhost.com/api/v1/workspaces/$WORKSPACE_ID/api-tokens \
  -H "Authorization: Bearer $SESSION_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{"name":"ci","scopes":["members:read"],"expiresInDays":30}'
  • scopes is required: at least one of members:read and subscription:read, with no repeats. There is no default, so a token never gets more access than you asked for.
  • Every token expires. expiresInDays defaults to 90 and may be 1 to 365. To renew, create a new token.
  • You may hold up to 25 active tokens per workspace. Creating one beyond that gets 409.
  • Creating tokens is rate limited per user, and a limited request gets 429.
  • PerkHost stores only a hash of the token. Lists show the first 12 characters as the prefix.

Use a token

Send it as Authorization: Bearer phk_.... A token works only on routes that accept tokens, and only if it carries the scope the route needs. On top of its scope, it acts with the role its creator holds in that workspace at the moment of the request. Today there are two scopes, one per route:

API token scopes
ScopeRouteRoles that may call it
members:readGET /workspaces/{workspaceId}/membersowner, admin, developer
subscription:readGET /workspaces/{workspaceId}/subscriptionowner, admin, billing
Read the subscription with a token
curl -sS https://api.perkhost.com/api/v1/workspaces/$WORKSPACE_ID/subscription \
  -H "Authorization: Bearer $PERKHOST_TOKEN"

A token without the scope gets 403 with the message "API token lacks the required scope" and the scope. On any other route a token gets 401 with the message "API tokens are not accepted on this route". A token used on another workspace gets 404. Managing tokens is session-only on purpose, so a leaked token cannot create more tokens or hide itself.

List and revoke

  • GET /workspaces/{workspaceId}/api-tokens lists your active tokens, newest first, with id, name, prefix, scopes, createdAt, lastUsedAt and expiresAt.
  • DELETE /workspaces/{workspaceId}/api-tokens/{tokenId} revokes one and answers 204, also when it was already revoked.

Tokens of other members

Owners and admins can list every active token in the workspace and revoke them, so a departing or compromised member's tokens can be cut without removing the member. GET /workspaces/{workspaceId}/member-api-tokens pages through the tokens, newest first, and each item names its owner. It never returns a token or its hash. DELETE /workspaces/{workspaceId}/member-api-tokens/{tokenId} revokes one; only an owner can revoke a token held by an owner, and an admin gets 403 for one. Both are session-only: a token gets 401, and a developer, billing member or client gets 403.

When a token stops working

  • It was revoked, by you or by an owner or admin, or it expired.
  • Its creator was removed from the workspace.
  • Its creator reset their password, or turned a two-factor factor on or off.
  • A refresh token of its creator was used again after it had been rotated. Tokens created in that session are revoked, and so are all the creator's tokens unless the reuse came within 5 seconds of the rotation.
  • Its creator passed the two-factor enrolment deadline without a second factor. The token then gets 403.

The CLI signs in with one of these tokens. See "The perkhost command".