SDK quickstart
1. Install the Python SDK
Our Python SDK is included with the Anyscale CLI. You can install it from pip.
pip install anyscale
2. Authentication
Anyscale authenticates API requests using a token, which can be issued either with the anyscale login
command in the CLI or manual generation of an API key.
-
Use
anyscale login
The CLI command fetches and updates the token in the local credential file (
~/.anyscale/credentials.json
). The SDK will use this token to authenticate its requests. This is recommended for interactive development. -
Use
ANYSCALE_CLI_TOKEN
environment variableManually generate your API key here and set it in this environment variable. The SDK checks the environment variable first before the credentials file. This is recommended for CI/CD integration and other production use case.
API keys issued by anyscale login
expire after 7 days. If you want them to persist longer or not to expire, use --expire-in-days=<# of days>
or --no-expire
flags.
from anyscale import AnyscaleSDK
sdk = AnyscaleSDK() # read the token from ~/.anyscale/credentials.json
Note that you can also supply a token to the SDK: sdk = AnyscaleSDK("your_api_key_here")
.
Be sure to keep your API key private and secure to prevent unwanted access to your Anyscale resources.
3. Test it out
To verify that everything is working, try creating a new Project using the SDK.
from anyscale import AnyscaleSDK
sdk = AnyscaleSDK()
create_project_response = sdk.create_project({
"name": "test-sdk-project",
"description": "Created from the Anyscale SDK"
})
print(f"Project created successfully: https://console.anyscale.com/projects/{create_project_response.result.id}")
If everything is working, you should see the success statement as output.
Project created successfully: https://console.anyscale.com/projects/prj_1234
4. Next Steps
After you have successfully set up the Python SDK, you may start interacting with Anyscale from your Python scripts. Note that only AnyscaleSDK
and imports under anyscale.sdk
are considered public.