Python SDK (v0.28.0)
Initializing
Guide on how to initialize the WriftAI Python client
This section demonstrates how to initialize the WriftAI Python client with practical examples.
Basic Initialization
If your environment variables are set (WRIFTAI_ACCESS_TOKEN and optionally WRIFTAI_API_BASE_URL),
you can initialize the client without passing any options.
from wriftai import Client
wriftai = Client()This automatically uses:
WRIFTAI_ACCESS_TOKENenvironment variable if present; otherwise requests are unauthenticatedWRIFTAI_API_BASE_URLenvironment variable if present; otherwise the default API base URL is used- Default configuration for the sync and async
httpxclients along with a request timeout of 10 seconds. - Up to 2 automatic retries on transient errors (429 rate limits and 5xx server errors) with exponential backoff.
Custom Initialization
If you want full control or are not using environment variables, you can provide options manually.
Use a Specific API Base URL
from wriftai import Client
wriftai = Client(api_base_url="https://api.wrift.ai/v1")Provide an Access Token Directly
from wriftai import Client
wriftai = Client(access_token="your_access_token_here")Add or Override Request Headers
from wriftai import Client
wriftai = Client(
headers={
'X-App-Name': 'my-app',
'X-Environment': 'production',
# You can override defaults too:
# "Authorization": 'Bearer <custom-token>',
}
)Configure Retries
By default the client retries up to 2 times on 429 (rate limit) and 5xx (server error) responses. For 429 responses the client waits until the server's x-ratelimit-reset time; for 5xx responses it uses exponential backoff starting at 0.5 seconds and capping at 8 seconds.
from wriftai import Client
# Increase retries
wriftai = Client(max_retries=5)
# Disable retries entirely
wriftai = Client(max_retries=0)Provide Custom httpx Clients
import httpx
from wriftai import Client
wriftai = Client(
http_client=httpx.Client(timeout=httpx.Timeout(30.0)),
async_http_client=httpx.AsyncClient(timeout=httpx.Timeout(30.0)),
)