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

# Authentication

> How to authenticate with the Xshot API

## API Key

All Xshot API requests require an API key. You can pass it in three ways:

### Query String

```bash theme={null}
curl "https://api.xshot.fun/twitter/user/info?username=elonmusk&api_key=YOUR_KEY"
```

Best for quick testing in the browser.

### Header

```bash theme={null}
curl -H "x-api-key: YOUR_KEY" "https://api.xshot.fun/twitter/user/info?username=elonmusk"
```

Recommended for production applications.

### Bearer Token

```bash theme={null}
curl -H "Authorization: Bearer YOUR_KEY" "https://api.xshot.fun/twitter/user/info?username=elonmusk"
```

Standard OAuth-style bearer authentication.

## WebSocket Authentication

For WebSocket connections, pass the API key as a query parameter:

```
wss://api.xshot.fun/ws?api_key=YOUR_KEY
```

Connections without a valid key are immediately closed with code `4001`.

## Unauthenticated Request

```bash theme={null}
curl "https://api.xshot.fun/twitter/user/info?username=elonmusk"
```

**Response (401):**

```json theme={null}
{
  "error": "Invalid or missing API key"
}
```

## Code Examples

<CodeGroup>
  ```javascript JavaScript (fetch) theme={null}
  const API_KEY = "YOUR_KEY";

  // Using query string
  const res = await fetch(
    `https://api.xshot.fun/twitter/user/info?username=elonmusk&api_key=${API_KEY}`
  );
  const user = await res.json();
  console.log(user.followers_count); // 237614000
  ```

  ```javascript JavaScript (header) theme={null}
  const res = await fetch(
    "https://api.xshot.fun/twitter/user/info?username=elonmusk",
    { headers: { "x-api-key": "YOUR_KEY" } }
  );
  const user = await res.json();
  ```

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

  # Using header
  response = requests.get(
      "https://api.xshot.fun/twitter/user/info",
      params={"username": "elonmusk"},
      headers={"x-api-key": "YOUR_KEY"}
  )
  user = response.json()
  print(user["followers_count"])  # 237614000
  ```

  ```python Python (query string) theme={null}
  import requests

  response = requests.get(
      "https://api.xshot.fun/twitter/user/info",
      params={"username": "elonmusk", "api_key": "YOUR_KEY"}
  )
  user = response.json()
  ```
</CodeGroup>

## WebSocket Authentication Example

```javascript theme={null}
const ws = new WebSocket("wss://api.xshot.fun/ws?api_key=YOUR_KEY");

ws.onopen = () => {
  console.log("Connected and authenticated");
};

ws.onclose = (event) => {
  if (event.code === 4001) {
    console.error("Authentication failed");
  }
};
```
