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

<Frame>
  <img src="https://mintcdn.com/portkey-docs/Hf1XgyjG_b79ym4Q/images/api-ref/a-r-1.gif?s=92fab8616e49938e9e892435c3227c0e" width="800" height="453" data-path="images/api-ref/a-r-1.gif" />
</Frame>

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 title="JWT-based Authentication" href="#jwt-based-authentication">
  You can also authenticate Portkey using JWT Tokens. Learn more here
</Card>

## Authentication with SDKs

### Portkey SDKs

<Tabs>
  <Tab title="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>

  <Tab title="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>

  <Tab title="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!" }
        ]
      }'
    ```
  </Tab>
</Tabs>

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

<Info>
  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.
</Info>

<Tabs>
  <Tab title="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>

  <Tab title="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'
    )
    ```
  </Tab>
</Tabs>

Read more [here](/integrations/llms/openai).

## 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 title="JWT Token Authentication" href="/product/enterprise-offering/org-management/jwt">
  Learn how to implement JWT-based authentication with Portkey
</Card>

<Note>
  <b>Interested in adding JWT authentication to your Portkey plan?</b>

  [Contact our sales team](https://portkey.sh/jwt) to discuss pricing and implementation details.
</Note>
