tokens

client.tokens

Access to API tokens.

Classes

Name Description
TokensError Raised when tokens cannot be created or revoked.

TokensError

client.tokens.TokensError()

Raised when tokens cannot be created or revoked.

Functions

Name Description
create_token Create an API token.
list_tokens List tokens of the given type.
revoke_token Revoke a token.

create_token

client.tokens.create_token(name, scope, *, expires_at=None)

Create an API token.

Parameters

Name Type Description Default
name str The name of the token. Will be modified by the server to enforce uniqueness. required
scope TokenScope See the enumeration of allowed values above. required
expires_at Optional[datetime] The expiration date of the token. Default: +30 minutes. None

Returns

Name Type Description
Token

Raises

Name Type Description
TokensError If the token creation request fails.

Examples

>>> token = create_token("api-token", TokenScope._)

Creates a new token with maximum scope and a duration of 30 minutes.

list_tokens

client.tokens.list_tokens(type, *, page=1, per_page=None)

List tokens of the given type.

An token listing consists of a sequence of “pages”, or batches, of lists of tokens. This function returns a Pager object that represents one page of the listing. The object also serves as an iterator over all tokens from that page to the last page, and it can be indexed to get all or a subset of tokens from that page to the last page.

Parameters

Name Type Description Default
type str “api” or “session”. required
page int Which page of results to retrieve. 1-based. 1
per_page int How many results to include on each page. None

Returns

Name Type Description
Pager for Tokens

Raises

Name Type Description
TokensError If the token listing request fails.

Examples

>>> for token in list_tokens("api"):
...     print(token)

Prints representations of all of the current user’s API tokens.

revoke_token

client.tokens.revoke_token(token)

Revoke a token.

Parameters

Name Type Description Default
token Token or str A token identified by Token instance or token_id. required

Raises

Name Type Description
TokensError If the token revocation request fails.

Examples

>>> revoke_token(token)