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

# JWT Authentication

> Generate a JWT (JSON Web Token) for secure frontend WebSocket connections. JWTs allow your client-side code to connect without exposing your API key.

## `POST` `/authentication/jwt`

Generate a JWT (JSON Web Token) for secure frontend WebSocket connections. JWTs allow your client-side code to connect without exposing your API key.

**Security model:** Generate JWTs server-side using your API key, then pass the `jwtKey` to your frontend. The JWT can be scoped to specific creators (`allowed_creators`) and limited to a set number of concurrent connections.

**Expiry:** Tokens expire after `expire_after` seconds (default: 3600 = 1 hour). After expiry, the client must request a new token from your backend.

**Use cases:** Web applications where the frontend connects directly to `wss://api.tik.tools?uniqueId=USERNAME&jwtKey=TOKEN`. This keeps your API key on the server while letting browsers connect to the WebSocket.

### Parameters

| Parameter          | Type      | Required | Description                                |
| ------------------ | --------- | -------- | ------------------------------------------ |
| `expire_after`     | number    | No       | Seconds until expiry (default: 3600)       |
| `allowed_creators` | string\[] | No       | Restrict to specific creators              |
| `max_websockets`   | number    | No       | Max concurrent WS connections (default: 1) |

### Response

```json theme={null}
{ "status_code": 0, "data": { "token": "eyJ..." } }
```

### Examples

<CodeGroup>
  ```javascript Node.js theme={null}
  const res = await fetch('https://api.tik.tools/authentication/jwt?apiKey=YOUR_KEY', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ expire_after: 3600, allowed_creators: ['streamer1'] })
  });
  const { data } = await res.json();
  // Use data.token for WebSocket: wss://api.tik.tools?jwtKey=TOKEN
  ```

  ```python Python theme={null}
  import requests
  res = requests.post('https://api.tik.tools/authentication/jwt',
      params={'apiKey': 'YOUR_KEY'},
      json={'expire_after': 3600, 'allowed_creators': ['streamer1']})
  token = res.json()['data']['token']
  # Use token: wss://api.tik.tools?jwtKey=TOKEN
  ```

  ```bash cURL theme={null}
  curl -X POST "https://api.tik.tools/authentication/jwt?apiKey=YOUR_KEY" \
    -H "Content-Type: application/json" \
    -d '{"expire_after": 3600, "allowed_creators": ["streamer1"]}'
  ```

  ```java Java theme={null}
  HttpClient client = HttpClient.newHttpClient();
  String json = "{\"expire_after\": 3600, \"allowed_creators\": [\"streamer1\"]}";
  HttpRequest req = HttpRequest.newBuilder()
      .uri(URI.create("https://api.tik.tools/authentication/jwt?apiKey=YOUR_KEY"))
      .header("Content-Type", "application/json")
      .POST(HttpRequest.BodyPublishers.ofString(json))
      .build();
  HttpResponse<String> res = client.send(req, HttpResponse.BodyHandlers.ofString());
  // Parse token from response body
  ```

  ```go Go theme={null}
  payload := bytes.NewBufferString(`{"expire_after":3600,"allowed_creators":["streamer1"]}`)
  resp, err := http.Post(
      "https://api.tik.tools/authentication/jwt?apiKey=YOUR_KEY",
      "application/json", payload)
  if err != nil { log.Fatal(err) }
  defer resp.Body.Close()
  body, _ := io.ReadAll(resp.Body)
  fmt.Println(string(body))
  ```

  ```csharp C# theme={null}
  using var client = new HttpClient();
  var json = new StringContent(
      @"{""expire_after"":3600,""allowed_creators"":[""streamer1""]}",
      Encoding.UTF8, "application/json");
  var res = await client.PostAsync(
      "https://api.tik.tools/authentication/jwt?apiKey=YOUR_KEY", json);
  Console.WriteLine(await res.Content.ReadAsStringAsync());
  ```
</CodeGroup>
