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

# Authentication

All requests to the Fortytwo API require authentication using an API key. This guide explains how to use and manage your API keys securely.

<Callout icon="key" color="#FFC107" iconType="regular">
  **Keep your API key secure!** Your API key should be treated like a password. Never share it publicly or commit it to version control systems.
</Callout>

## Using Your API Key

Include your API key in the `Authorization` header of every API request using the Bearer authentication scheme:

```bash theme={null}
Authorization: Bearer YOUR_FORTYTWO_API_KEY
```

### Example Request

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.fortytwo.network/v1/models \
    -H "Authorization: Bearer YOUR_FORTYTWO_API_KEY"
  ```

  ```python Python theme={null}
  import requests

  api_key = "YOUR_FORTYTWO_API_KEY"
  headers = {
      "Authorization": f"Bearer {api_key}"
  }

  response = requests.get(
      "https://api.fortytwo.network/v1/models",
      headers=headers
  )
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const apiKey = 'YOUR_FORTYTWO_API_KEY';

  fetch('https://api.fortytwo.network/v1/models', {
    headers: {
      'Authorization': `Bearer ${apiKey}`
    }
  })
    .then(response => response.json())
    .then(data => console.log(data));
  ```
</CodeGroup>

## Authentication Errors

If authentication fails, refer to the '[Errors](/docs/api-errors#authentication-errors/api-errors)' page for details on error codes.

## Store API Keys Securely

Store your API key in environment variables rather than hardcoding it:

<Tabs>
  <Tab title="Python">
    Python can not read from .env file by default. You have 2 options:

    <Tabs>
      <Tab title="Option 1: Add to machine env">
        ```bash theme={null}
        export FORTYTWO_API_KEY=YOUR_FORTYTWO_API_KEY
        ```

        ```python Python  theme={null}
        import os  

        api_key = os.getenv('FORTYTWO_API_KEY')
        ```
      </Tab>

      <Tab title="Option 2: Read with python-dotenv package">
        ```bash Bash theme={null}
        # .env file
        FORTYTWO_API_KEY=YOUR_FORTYTWO_API_KEY
        ```

        ```bash theme={null}
        pip install python-dotenv  
        ```

        ```python Python theme={null}
        from dotenv import load_dotenv  
        import os  

        load_dotenv()  

        api_key = os.getenv('FORTYTWO_API_KEY') 
        ```
      </Tab>
    </Tabs>
  </Tab>

  <Tab title="JavaScript">
    ```bash Bash theme={null}
    # .env file
    FORTYTWO_API_KEY=YOUR_FORTYTWO_API_KEY
    ```

    ```javascript JavaScript theme={null}
    // Using dotenv
    require('dotenv').config();
    const apiKey = process.env.FORTYTWO_API_KEY;
    ```
  </Tab>
</Tabs>

## API Key Management

### Revoking Keys

<Callout icon="key" color="#FFC107" iconType="regular">
  Revoking a key immediately invalidates it. Any applications using the revoked key will stop working. You cannot revert this action.
</Callout>

## OpenAI Compatibility

The Fortytwo API uses the same authentication format as OpenAI's API. If you're **migrating from OpenAI**, you can simply **replace your OpenAI API key with a Fortytwo API key**:

<CodeGroup>
  ```python Python theme={null}
  # OpenAI
  from openai import OpenAI
  client = OpenAI(api_key="sk-openai-key")

  # Fortytwo
  from openai import OpenAI
  client = OpenAI(
      api_key="YOUR_FORTYTWO_API_KEY",
      base_url="https://api.fortytwo.network/v1"
  )
  ```

  ```javascript JavaScript theme={null}
  // OpenAI
  import OpenAI from "openai";
  const client = new OpenAI({
    apiKey: "sk-openai-key"
  });

  // Fortytwo
  import OpenAI from "openai";
  const client = new OpenAI({
    apiKey: "YOUR_FORTYTWO_API_KEY",
    baseURL: "https://api.fortytwo.network/v1"
  });
  ```
</CodeGroup>

## Troubleshooting

If you experience authentication issues:

* Verify that your API key is written correctly.
* Make sure that the `Authorization` header is properly formatted.
* Contact support through [<Icon icon="discord" /> Discord <Icon icon="arrow-up-right" />](https://discord.com/invite/fortytwo).
