> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portkey.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

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](https://app.portkey.ai/signup) or [log in](https://app.portkey.ai/login) to your Portkey account. Grab your account's API key from the "Settings" page.

![](https://mintcdn.com/portkey-docs/Hf1XgyjG_b79ym4Q/images/api-ref/a-r-1.gif?s=92fab8616e49938e9e892435c3227c0e)

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.

> [card] [JWT-based Authentication](#jwt-based-authentication)
>
> You can also authenticate Portkey using JWT Tokens. Learn more here

## Authentication with SDKs

### Portkey SDKs

> [tab] NodeJS SDK
>
> ```ts theme={"system"}
> import Portkey from 'portkey-ai'
>
> const portkey = new Portkey({
>     apiKey: "PORTKEY_API_KEY", // Replace with your actual API key
>     provider: "@openai-prod"   // Optional: AI Provider slug from Model Catalog
> })
>
> const chatCompletion = await portkey.chat.completions.create({
>     messages: [{ role: 'user', content: 'Say this is a test' }],
>     model: 'gpt-4o',
> });
>
> console.log(chatCompletion.choices);
> ```

> [tab] Python SDK
>
> ```python theme={"system"}
> from portkey_ai import Portkey
>
> client = Portkey(
>     api_key="PORTKEY_API_KEY",  # Replace with your actual API key
>     provider="@openai-prod"    # Optional: AI Provider slug from Model Catalog
> )
>
> 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"])
> ```

> [tab] cURL
>
> ```sh theme={"system"}
> curl https://api.portkey.ai/v1/chat/completions \
>   -H "Content-Type: application/json" \
>   -H "x-portkey-api-key: $PORTKEY_API_KEY" \
>   -H "x-portkey-provider: @openai-prod" \
>   -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:

> [!NOTE]
>
> 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.

> [tab] NodeJS
>
> ```js theme={"system"}
> 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();
> ```

> [tab] Python
>
> ```python theme={"system"}
> 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](https://docs.portkey.ai/docs/integrations/llms/openai.md).

## 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

> [card] [JWT Token Authentication](https://docs.portkey.ai/docs/product/enterprise-offering/org-management/jwt.md)
>
> Learn how to implement JWT-based authentication with Portkey

> [!NOTE]
>
> **Interested in adding JWT authentication to your Portkey plan?**
>
> [Contact our sales team](https://portkey.sh/jwt) to discuss pricing and implementation details.

***

> [card] [Portkey is now PRISMA AIRS AI Gateway. See it in action.](https://www.paloaltonetworks.in/ai-security/ai-gateway?utm_source=portkey\&utm_medium=referral\&utm_campaign=prisma_airs\&utm_content=docs_nav#contact)
>
> Contact Us


## Related topics

- [Authentication](https://docs.portkey.ai/docs/product/mcp-gateway/authentication.md)
- [JWT Authentication](https://docs.portkey.ai/docs/product/enterprise-offering/org-management/jwt.md)
- [Cursor](https://docs.portkey.ai/docs/integrations/mcp-clients/cursor.md)
- [Overview](https://docs.portkey.ai/docs/product/enterprise-offering/org-management/scim/scim.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.