# Authentication

### Overview

The Nansen API uses API key authentication. All requests must include your API key in the `apikey` header.

### Getting Your API Key

1. Log in to your [Nansen Account](https://app.nansen.ai/auth/agent-setup)
2. Generate a new API key

### Using Your API Key

#### Header Format

```
apikey: YOUR_API_KEY
```

#### curl Example

```bash
curl -X POST 'https://api.nansen.ai/api/v1/smart-money/holdings' \
  -H 'Content-Type: application/json' \
  -H 'apikey: YOUR_API_KEY' \
  -d '{"chains": ["ethereum"]}'
```

#### Python Example

```python
import httpx

headers = {
    "Content-Type": "application/json",
    "apikey": "YOUR_API_KEY"
}

response = httpx.post(
    "https://api.nansen.ai/api/v1/smart-money/holdings",
    headers=headers,
    json={"chains": ["ethereum"]}
)
```

#### Python with Environment Variable

```python
import os
import httpx

API_KEY = os.environ.get("NANSEN_API_KEY")

headers = {
    "Content-Type": "application/json",
    "apikey": API_KEY
}

response = httpx.post(
    "https://api.nansen.ai/api/v1/smart-money/holdings",
    headers=headers,
    json={"chains": ["ethereum"]}
)
```

### Authentication Errors

#### 401 Unauthorized

Returned when the API key is missing or invalid:

```json
{
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Invalid or missing API key"
  }
}
```

**Solutions:**

* Verify your API key is correct
* Ensure the header name is `apikey` (lowercase)
* Check that the key hasn't been revoked

#### 403 Forbidden

Returned when the API key doesn't have permission for the requested resource:

```json
{
  "error": {
    "code": "FORBIDDEN",
    "message": "Insufficient permissions for this endpoint"
  }
}
```

**Solutions:**

* Verify your subscription tier includes access to this endpoint
* Contact support to upgrade your plan

### Key Management

If your key is compromised:

1. Immediately revoke it in your dashboard
2. Generate a new key
3. Update all applications using the old key


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nansen.ai/getting-started/authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
