Skip to content

API Tokens

Manage API tokens in the sandbox.

Get listing of current Public API tokens

Code example
import asyncio
from ptsandbox import Sandbox, SandboxKey

async def main():
    sandbox = Sandbox(...)
    await sandbox.ui.authorize()

    tokens = await sandbox.ui.get_api_tokens()
    print(tokens)

asyncio.run(main())

ptsandbox.sandbox.ui._tokens.TokensMixin.get_api_tokens async

get_api_tokens() -> SandboxTokensResponse

Get listing of current Public API tokens

Returns:

Raises:

  • SandboxException

    if not authorized or token refresh fails

  • ClientResponseError

    if the server returns an error status

  • ClientError

    on connection or transport errors

  • ValidationError

    if the response body does not match the expected model

Create a new Public API token

Code example
import asyncio
from ptsandbox import Sandbox, SandboxKey
from ptsandbox.models import TokenPermissions

async def main():
    sandbox = Sandbox(...)
    await sandbox.ui.authorize()

    token = await sandbox.ui.create_api_token(
        name="test-token",
        permissions=[
            TokenPermissions.SCAN_WITH_EXTENDED_SETTINGS,
            TokenPermissions.SCAN_WITH_PREDEFINED_SETTINGS,
        ],
        comment="test-comment",
    )
    print(token)

asyncio.run(main())

ptsandbox.sandbox.ui._tokens.TokensMixin.create_api_token async

create_api_token(
    name: str,
    permissions: list[TokenPermissions],
    comment: str = "",
) -> SandboxCreateTokenResponse

Create a new Public API token

Parameters:

  • name (str) –

    token name

  • permissions (list[TokenPermissions]) –

    permissions for the token

  • comment (str, default: '' ) –

    additional information about the token

Returns:

Raises:

  • SandboxException

    if not authorized or token refresh fails

  • ClientResponseError

    if the server returns an error status

  • ClientError

    on connection or transport errors

  • ValidationError

    if the response body does not match the expected model

Delete the Public API token

Code example
import asyncio
from ptsandbox import Sandbox, SandboxKey

async def main():
    sandbox = Sandbox(...)
    await sandbox.ui.authorize()

    await sandbox.ui.delete_api_token(token_id=1337)

asyncio.run(main())

ptsandbox.sandbox.ui._tokens.TokensMixin.delete_api_token async

delete_api_token(token_id: int) -> None

Delete the Public API token

Parameters:

  • token_id (int) –

    id of the PublicAPI token in the database

Raises:

  • SandboxException

    if not authorized or token refresh fails

  • ClientResponseError

    if the server returns an error status

  • ClientError

    on connection or transport errors