API Tokens
You can manage api tokens that are 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())
Response model in ptsandbox/models/ui/tokens.py
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())
Source code in ptsandbox/sandbox/sandbox_ui.py
async def create_api_token(
self,
name: str,
permissions: list[TokenPermissions],
comment: str = "",
) -> SandboxCreateTokenResponse:
"""
Create a new Public API token
Args:
name: token name
permissions: permissions for the token
comment: additional information about the token
Returns:
A model with information about the created token
"""
response = await self.http_client.post(
f"{self.key.ui_url}/public-api/tokens",
json={
"name": name,
"permissions": permissions,
"comment": comment,
},
)
response.raise_for_status()
return SandboxCreateTokenResponse.model_validate(await response.json())
Response model in ptsandbox/models/ui/tokens.py
Delete the 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()
await sandbox.ui.delete_api_token(token_id=1337)
asyncio.run(main())