Skip to main content
You can integrate Portrix into Python projects using the openai library (recommended), the requests library for lightweight HTTP calls, or httpx for async-first applications. All three approaches use the same base URL and Bearer token authentication, so you can choose whichever fits your stack.

Installation

Install the openai library to get the recommended client experience. Add requests or httpx if you prefer a lower-level HTTP approach.

Using the openai library

Create an OpenAI client pointed at Portrix’s base URL, then call it exactly as you would the standard OpenAI client:

Using environment variables

Keep your API key out of source code by loading it from the environment or a .env file:
Create a .env file at your project root (and add it to .gitignore):
Never commit your API key to version control. Use .env files locally and your platform’s secret manager (e.g. AWS Secrets Manager, Doppler, Vercel environment variables) in production.

Using requests

Use requests for scripts or environments where you prefer a plain HTTP call without the OpenAI SDK:

Using httpx

Use httpx.AsyncClient for async applications such as FastAPI services or async scripts:

Streaming in Python

Enable streaming by passing stream=True to the openai client. Iterate over the response chunks to print tokens as they arrive:

Error handling

Wrap API calls in a try/except block to handle rate limits, invalid requests, and other API errors gracefully:

Complete example

The following helper class wraps Portrix with automatic retry logic using exponential backoff:
Swap the model argument when constructing PortrixClient to change providers without touching any other code. This makes A/B testing different models straightforward.