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

# Integration guide

> Learn how to quickly integrate with HaiToken LLM gateway, supporting both OpenAI and Claude APIs

HaiToken LLM gateway is compatible with OpenAI and Anthropic Claude API protocols. You can get started in three steps: get your API key, call the API, and connect to AI tools.

## Step 1: Get your API key

1. Register and log in to the [HaiToken console](https://haitoken.ai)
2. Navigate to **Token management** in the left sidebar
3. Click **Create API Key**, enter a name, and generate your key
4. Copy and securely store the generated API key (it is shown only once)

<Warning>
  Keep your API key secure. Do not expose it in client-side code or public repositories.
</Warning>

## Step 2: Call the API

HaiToken offers two API formats. Choose the one that matches the model you want to use.

### OpenAI format

For OpenAI-compatible models, using the `POST /v1/chat/completions` protocol.

<CodeGroup>
  ```python Python theme={null}
  import requests

  url = "https://api.haitoken.ai/v1/chat/completions"
  headers = {
      "Authorization": "Bearer YOUR_API_KEY",
      "Content-Type": "application/json"
  }
  data = {
      "model": "gpt-4o",
      "messages": [
          {"role": "user", "content": "Hello, introduce yourself please"}
      ]
  }

  response = requests.post(url, json=data, headers=headers)
  print(response.json())
  ```

  ```java Java theme={null}
  import java.net.URI;
  import java.net.http.HttpClient;
  import java.net.http.HttpRequest;
  import java.net.http.HttpResponse;
  import java.net.http.HttpRequest.BodyPublishers;

  String apiKey = "YOUR_API_KEY";
  String body = """
      {
          "model": "gpt-4o",
          "messages": [
              {"role": "user", "content": "Hello, introduce yourself please"}
          ]
      }
      """;

  HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("https://api.haitoken.ai/v1/chat/completions"))
      .header("Authorization", "Bearer " + apiKey)
      .header("Content-Type", "application/json")
      .POST(BodyPublishers.ofString(body))
      .build();

  HttpClient client = HttpClient.newHttpClient();
  HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
  System.out.println(response.body());
  ```

  ```javascript Node.js theme={null}
  const response = await fetch("https://api.haitoken.ai/v1/chat/completions", {
    method: "POST",
    headers: {
      "Authorization": "Bearer YOUR_API_KEY",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "gpt-4o",
      messages: [
        { role: "user", content: "Hello, introduce yourself please" }
      ]
    })
  });

  const data = await response.json();
  console.log(data);
  ```

  ```curl cURL theme={null}
  curl -X POST "https://api.haitoken.ai/v1/chat/completions" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-4o",
      "messages": [
        {"role": "user", "content": "Hello, introduce yourself please"}
      ]
    }'
  ```
</CodeGroup>

### Claude format

For Anthropic Claude models, using the `POST /v1/messages` protocol.

<Note>
  When calling the Claude format API, include the `anthropic-version` header. The recommended value is `2023-06-01`.
</Note>

<CodeGroup>
  ```python Python theme={null}
  import requests

  url = "https://api.haitoken.ai/v1/messages"
  headers = {
      "Authorization": "Bearer YOUR_API_KEY",
      "Content-Type": "application/json",
      "anthropic-version": "2023-06-01"
  }
  data = {
      "model": "claude-sonnet-4-0",
      "max_tokens": 1024,
      "messages": [
          {"role": "user", "content": "Hello, introduce yourself please"}
      ]
  }

  response = requests.post(url, json=data, headers=headers)
  print(response.json())
  ```

  ```java Java theme={null}
  import java.net.URI;
  import java.net.http.HttpClient;
  import java.net.http.HttpRequest;
  import java.net.http.HttpResponse;
  import java.net.http.HttpRequest.BodyPublishers;

  String apiKey = "YOUR_API_KEY";
  String body = """
      {
          "model": "claude-sonnet-4-0",
          "max_tokens": 1024,
          "messages": [
              {"role": "user", "content": "Hello, introduce yourself please"}
          ]
      }
      """;

  HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("https://api.haitoken.ai/v1/messages"))
      .header("Authorization", "Bearer " + apiKey)
      .header("Content-Type", "application/json")
      .header("anthropic-version", "2023-06-01")
      .POST(BodyPublishers.ofString(body))
      .build();

  HttpClient client = HttpClient.newHttpClient();
  HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
  System.out.println(response.body());
  ```

  ```javascript Node.js theme={null}
  const response = await fetch("https://api.haitoken.ai/v1/messages", {
    method: "POST",
    headers: {
      "Authorization": "Bearer YOUR_API_KEY",
      "Content-Type": "application/json",
      "anthropic-version": "2023-06-01"
    },
    body: JSON.stringify({
      model: "claude-sonnet-4-0",
      max_tokens: 1024,
      messages: [
        { role: "user", content: "Hello, introduce yourself please" }
      ]
    })
  });

  const data = await response.json();
  console.log(data);
  ```

  ```curl cURL theme={null}
  curl -X POST "https://api.haitoken.ai/v1/messages" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -H "anthropic-version: 2023-06-01" \
    -d '{
      "model": "claude-sonnet-4-0",
      "max_tokens": 1024,
      "messages": [
        {"role": "user", "content": "Hello, introduce yourself please"}
      ]
    }'
  ```
</CodeGroup>

## Step 3: Connect to AI tools

Once you have your API key and the API is verified, you can connect HaiToken to popular AI coding tools.

### Cursor

1. Open Cursor, go to **Settings** > **Models**
2. Enter your API key in the **OpenAI API Key** field
3. Set **OpenAI Base URL** to `https://api.haitoken.ai/v1`
4. Save and select from the available models in the model list

### Claude Code

1. Set environment variables in your terminal:

```bash theme={null}
export ANTHROPIC_API_KEY="YOUR_API_KEY"
export ANTHROPIC_BASE_URL="https://api.haitoken.ai/"
```

2. Launch Claude Code to start using it.

### Open Code

1. Add haitoken directly from the OpenCode TUI:

Launch the OpenCode TUI
Type /connect
In the provider list, choose "Other" (custom endpoint)
Fill in:
Base URL: [https://api.haitoken.ai/v1](https://api.haitoken.ai/v1)
API Key: your haitoken API key
After saving, type /models and switch to the newly added model

2. Configure provider and base URL in the `opencode.json` file at your project root:

```json theme={null}
"provider": {
    "haitoken": {
      "models": {
        "deepseek-v4-flash": {
          "name": "deepseek-v4-flash"
        },
        "deepseek-v4-pro": {
          "name": "deepseek-v4-pro"
        },
      },
      "name": "haitoken",
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://api.haitoken.ai/v1"
      }
    }
  }
```

<Tip>
  For more AI tool integration guides, see the [AI tool integration](/docs/en/tools/claude-code) section.
</Tip>

## Next steps

* See the [API reference](/docs/en/api-reference/chat/openai-format) for complete endpoint parameters
* See [Model list](/docs/en/api-reference/models/list-models) for available models
* See [Rate limits](/docs/en/others/rate-limits) for API call limits
