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

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.

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

```bash cURL theme={null}
# 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 = {
	"token": YOUR_API_KEY
    "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?token=YOUR_API_KEY';
const apiKey = 'YOUR_API_KEY';

async function getNews() {
  try {
    const response = await axios.get(url, {
      headers: {
        '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)
	}

	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))
}
```

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