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

# Authentication

> How to authenticate your requests to the Benzinga API

The Benzinga API uses **API Keys** to authenticate requests. Your API Key is a unique identifier that grants access to specific data and features based on your subscription.

<Note>
  **Keep your API Key secure.** Do not share it in publicly accessible areas such as GitHub, client-side code, or unsecured communications. If you believe your key has been compromised, contact support immediately.
</Note>

You can view and manage your API Key in your [Benzinga Developer Console](https://www.benzinga.com/apis/licensing/register).

## Authentication Methods

The Benzinga API supports two methods for authentication. We strongly recommend using the **HTTP Header** method for production applications as it is more secure and prevents your key from appearing in URL logs.

### 1. HTTP Header (Recommended)

To authenticate via the header, include the `Authorization` header with the value `token <YOUR_API_KEY>`.

```http theme={null}
Authorization: token <YOUR_API_KEY>
```

### 2. Query Parameter

For quick testing or when header modification isn't possible, you can pass your key as a query parameter named `token`.

```http theme={null}
https://api.benzinga.com/api/v2/news?token=<YOUR_API_KEY>
```

## Code Examples

Here are production-ready examples for connecting to the Benzinga API in common languages.

<CodeGroup>
  ```bash cURL theme={null}
  # Recommended: Header Authentication
  curl -L 'https://api.benzinga.com/api/v2/news?pageSize=1' \
  -H 'Authorization: token YOUR_API_KEY' \
  -H 'Accept: application/json'

  # Alternative: Query Parameter
  curl -L 'https://api.benzinga.com/api/v2/news?pageSize=1&token=YOUR_API_KEY'
  ```

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

  url = "https://api.benzinga.com/api/v2/news"

  # Recommended: Header Authentication
  headers = {
      "Authorization": "token YOUR_API_KEY",
      "Accept": "application/json"
  }

  params = {
      "pageSize": 1
  }

  try:
      response = requests.get(url, headers=headers, params=params)
      response.raise_for_status() # Raise detailed error for 4xx/5xx responses
      data = response.json()
      print(data)
  except requests.exceptions.RequestException as e:
      print(f"Error fetching data: {e}")
  ```

  ```javascript Node.js theme={null}
  const axios = require('axios');

  const url = 'https://api.benzinga.com/api/v2/news';
  const apiKey = 'YOUR_API_KEY';

  async function getNews() {
    try {
      const response = await axios.get(url, {
        headers: {
          'Authorization': `token ${apiKey}`,
          'Accept': 'application/json'
        },
        params: {
          pageSize: 1
        }
      });
      
      console.log(response.data);
    } catch (error) {
      if (error.response) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx
        console.error('Error Status:', error.response.status);
        console.error('Error Data:', error.response.data);
      } else {
        console.error('Error Message:', error.message);
      }
    }
  }

  getNews();
  ```

  ```go Go theme={null}
  package main

  import (
  	"fmt"
  	"io/ioutil"
  	"net/http"
  )

  func main() {
  	url := "https://api.benzinga.com/api/v2/news?pageSize=1"
  	apiKey := "YOUR_API_KEY"

  	req, err := http.NewRequest("GET", url, nil)
  	if err != nil {
  		panic(err)
  	}

  	// Recommended: Header Authentication
  	req.Header.Add("Authorization", fmt.Sprintf("token %s", apiKey))
  	req.Header.Add("Accept", "application/json")

  	client := &http.Client{}
  	resp, err := client.Do(req)
  	if err != nil {
  		panic(err)
  	}
  	defer resp.Body.Close()

  	if resp.StatusCode >= 400 {
  		fmt.Printf("Error: Status Code %d\n", resp.StatusCode)
  		return
  	}

  	body, err := ioutil.ReadAll(resp.Body)
  	if err != nil {
  		panic(err)
  	}

  	fmt.Println(string(body))
  }
  ```
</CodeGroup>

## Troubleshooting

Common authentication errors and how to resolve them.

| Status Code | Message        | Possible Cause           | Resolution                                                                                                                             |
| :---------- | :------------- | :----------------------- | :------------------------------------------------------------------------------------------------------------------------------------- |
| **401**     | `Unauthorized` | Invalid API Key          | Check that your API key is correct and has not been regenerated. ensure no extra spaces are copied.                                    |
| **401**     | `Unauthorized` | Missing API Key          | Ensure the `Authorization` header is formatted correctly as `token <KEY>` or the `token` parameter is present.                         |
| **403**     | `Forbidden`    | Insufficient Permissions | Your API key is valid, but your plan does not include access to the requested endpoint. Contact sales or support to upgrade your plan. |
