# Getting started

Source: [Getting started](<https://docs.gptunnel.ru/en/>)

<a id="_top"></a>
[Choose an API](<https://docs.gptunnel.ru/en/getting-started/choose-api/>) · [Pricing](<https://docs.gptunnel.ru/en/getting-started/pricing/>) · [Check your balance](<https://docs.gptunnel.ru/en/getting-started/balance/>)

<a id="get-an-api-key"></a>
## Get an API key

Open the **API** tab in your [GPTunneL account](<https://gptunnel.ru/profile>) and create a key. Both personal and business profiles support API keys.

When you create or edit a key, you can select the models it may use. Model catalogs then show only the selected models. A request to any other model returns HTTP `403` with `model_not_allowed` (Creative Lab returns `code: 29`, transcription returns `code: 19`). The restriction applies across the public API, including text to speech (`tts2`), face swapping (`faceswap`) and transcription (`whisper`). You can select these models in the same list. Leave the list empty to allow all models.

> **Note: Which balance pays for the request?**
>
> A personal key uses your personal balance. A business key uses the organization’s balance by default. To pay for this request from your personal balance with a business key, set `useWalletBalance: true` at the top level of the JSON body alongside `model` and `messages`.

<a id="send-your-first-request"></a>
## Send your first request

Send an authenticated request to `chat/completions` to get a response from a language model. Find the model IDs available to your key in the [model catalog](<https://docs.gptunnel.ru/en/api/models/>).

`POST https://gptunnel.ru/v1/chat/completions`

### Request headers

| Parameter | Type | Required? | Description |
| --- | --- | --- | --- |
| `Authorization` | `string` | Required | Your API key. |

### Request body

| Parameter | Type | Required? | Description |
| --- | --- | --- | --- |
| `model` | `string` | Required | Model ID from GET /v1/models, such as gpt-6-luna. |
| `messages` | `array` | Required | Messages that provide the conversation context. |
| `max_completion_tokens` | `number` | Optional | Maximum number of response tokens. The example uses 128. |
| `useWalletBalance` | `boolean` | Optional | Set to true to use your personal balance with a business key. |

See [Check your balance](<https://docs.gptunnel.ru/en/getting-started/balance/>) for more on choosing a balance.

<a id="example-request"></a>
### Example request

Set the `GPTUNNEL_API_KEY` environment variable and choose which balance to use. The model request incurs a charge. Check `usage.total_cost` in your response for the amount.

<a id="tab-panel-0-0"></a>
**Default balance**

<a id="tab-panel-1-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

body = {
    'model': "gpt-6-luna",
    'messages': [{"role":"user","content":"What's 2 + 2? Answer with a digit."}],
    'max_completion_tokens': 128,
}
request = Request(
    'https://gptunnel.ru/v1/chat/completions',
    method='POST',
    headers={
        'Authorization': os.environ['GPTUNNEL_API_KEY'],
        'Content-Type': 'application/json',
    },
    data=json.dumps(body).encode('utf-8'),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response)['choices'][0]['message']['content'])
```

<a id="tab-panel-1-1"></a>
**cURL**

```bash
curl --fail-with-body --silent --show-error \
  --url 'https://gptunnel.ru/v1/chat/completions' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --json '{
  "model": "gpt-6-luna",
  "messages": [
    {
      "role": "user",
      "content": "What'\''s 2 + 2? Answer with a digit."
    }
  ],
  "max_completion_tokens": 128
}'
```

<a id="tab-panel-1-2"></a>
**JavaScript (Node.js)**

```javascript
const apiKey = process.env.GPTUNNEL_API_KEY
if (!apiKey) throw new Error("Set GPTUNNEL_API_KEY")

const response = await fetch('https://gptunnel.ru/v1/chat/completions', {
  method: 'POST',
  headers: {
    Authorization: apiKey,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  "model": "gpt-6-luna",
  "messages": [
    {
      "role": "user",
      "content": "What's 2 + 2? Answer with a digit."
    }
  ],
  "max_completion_tokens": 128
}),
})

if (!response.ok) throw new Error(await response.text())
console.log((await response.json()).choices[0].message.content)
```

<a id="tab-panel-0-1"></a>
**Personal balance**

<a id="tab-panel-2-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

body = {
    'model': "gpt-6-luna",
    'messages': [{"role":"user","content":"What's 2 + 2? Answer with a digit."}],
    'max_completion_tokens': 128,
    'useWalletBalance': True,
}
request = Request(
    'https://gptunnel.ru/v1/chat/completions',
    method='POST',
    headers={
        'Authorization': os.environ['GPTUNNEL_API_KEY'],
        'Content-Type': 'application/json',
    },
    data=json.dumps(body).encode('utf-8'),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response)['choices'][0]['message']['content'])
```

<a id="tab-panel-2-1"></a>
**cURL**

```bash
curl --fail-with-body --silent --show-error \
  --url 'https://gptunnel.ru/v1/chat/completions' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --json '{
  "model": "gpt-6-luna",
  "messages": [
    {
      "role": "user",
      "content": "What'\''s 2 + 2? Answer with a digit."
    }
  ],
  "max_completion_tokens": 128,
  "useWalletBalance": true
}'
```

<a id="tab-panel-2-2"></a>
**JavaScript (Node.js)**

```javascript
const apiKey = process.env.GPTUNNEL_API_KEY
if (!apiKey) throw new Error("Set GPTUNNEL_API_KEY")

const response = await fetch('https://gptunnel.ru/v1/chat/completions', {
  method: 'POST',
  headers: {
    Authorization: apiKey,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  "model": "gpt-6-luna",
  "messages": [
    {
      "role": "user",
      "content": "What's 2 + 2? Answer with a digit."
    }
  ],
  "max_completion_tokens": 128,
  "useWalletBalance": true
}),
})

if (!response.ok) throw new Error(await response.text())
console.log((await response.json()).choices[0].message.content)
```

<a id="response-excerpt"></a>
### Response excerpt

Read `choices[0].message.content` for the text of the first reply. If your request is rejected, see [Troubleshoot API errors](<https://docs.gptunnel.ru/en/getting-started/api-errors/>).

```json
{
  "model": "gpt-6-luna",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "4"
      },
      "finish_reason": "stop"
    }
  ]
}
```
