To ensure secure access to Portkey’s APIs, authentication is required for all requests. This guide provides the necessary steps to authenticate your requests using the Portkey API key, regardless of whether you are using the SDKs for Python and JavaScript, the OpenAI SDK, or making REST API calls directly.
Obtaining Your API Key
Create or log in to your Portkey account. Grab your account’s API key from the “Settings” page.
Based on your access level, you might see the relevant permissions on the API key modal - tick the ones you’d like, name your API key, and save it.
JWT-based Authentication
You can also authenticate Portkey using JWT Tokens. Learn more here
Authentication with SDKs
Portkey SDKs
import Portkey from 'portkey-ai'
const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY", // Replace with your actual API key
virtualKey: "VIRTUAL_KEY" // Optional: Use for virtual key management
})
const chatCompletion = await portkey.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'gpt-4o',
});
console.log(chatCompletion.choices);
import Portkey from 'portkey-ai'
const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY", // Replace with your actual API key
virtualKey: "VIRTUAL_KEY" // Optional: Use for virtual key management
})
const chatCompletion = await portkey.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'gpt-4o',
});
console.log(chatCompletion.choices);
from portkey_ai import Portkey
client = Portkey(
api_key="PORTKEY_API_KEY", # Replace with your actual API key
virtual_key="VIRTUAL_KEY" # Optional: Use if virtual keys are set up
)
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": "Say this is a test"}],
model='gpt-4o'
)
print(chat_completion.choices[0].message["content"])
curl https://api.portkey.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "x-portkey-api-key: $PORTKEY_API_KEY" \
-H "x-portkey-virtual-key: $VIRTUAL_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "Hello!" }
]
}'
OpenAI SDK
When integrating Portkey through the OpenAI SDK, modify the base URL and add the x-portkey-api-key
header for authentication. Here’s an example of how to do it:
We use the createHeaders
helper function from the Portkey SDK here to easily create Portkey headers.
You can pass the raw headers (x-portkey-api-key
, x-portkey-provider
) directly in the defaultHeaders
param as well.
import OpenAI from 'openai';
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'
const openai = new OpenAI({
apiKey: 'OPENAI_API_KEY',
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({
provider: "openai",
apiKey: "PORTKEY_API_KEY"
})
});
async function main() {
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'gpt-4o',
});
console.log(chatCompletion.choices);
}
main();
import OpenAI from 'openai';
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'
const openai = new OpenAI({
apiKey: 'OPENAI_API_KEY',
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({
provider: "openai",
apiKey: "PORTKEY_API_KEY"
})
});
async function main() {
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'gpt-4o',
});
console.log(chatCompletion.choices);
}
main();
from openai import OpenAI
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders
openai_client = OpenAI(
base_url=PORTKEY_GATEWAY_URL,
default_headers=createHeaders(
api_key="PORTKEY-API-KEY",
provider="openai"
)
)
response = openai_client.chat.completions.create(
messages=[{'role': 'user', 'content': 'Say this is a test'}],
model='gpt-4bo'
)
Read more here.
JWT-based Authentication
Portkey supports JWT-based authentication as a secure alternative to API Key authentication. With JWT authentication, clients can authenticate API requests using a JWT token that is validated against a configured JWKS (JSON Web Key Set).
This enterprise-grade authentication method is available as an add-on to any Portkey plan. JWT authentication provides enhanced security through:
- Temporary, expiring tokens
- Fine-grained permission scopes
- User identity tracking
- Centralized authentication management
JWT Token Authentication
Learn how to implement JWT-based authentication with Portkey
Interested in adding JWT authentication to your Portkey plan?Contact our sales team to discuss pricing and implementation details.