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

# Completions

> Generate text completions from a prompt

# Completions

The completions endpoint generates text from a single prompt. For conversational use cases, prefer [Chat Completions](/api-reference/chat-completions).

<Note>
  Edge endpoints accept your `pg_*` API key as a bearer token. See [Authentication](/authentication) for details. The cURL examples below pin Toronto (`yto-01`) for concreteness — substitute another region or discover the fastest one via `GET https://autorouter.polargrid.ai/v1/route`. See [API Overview](/api-reference/overview#picking-a-region) for both patterns.
</Note>

## Create Completion

```
POST /v1/completions
```

Generate a completion for the given prompt.

### Request Body

| Parameter           | Type    | Required | Default | Description                     |
| ------------------- | ------- | -------- | ------- | ------------------------------- |
| `prompt`            | string  | Yes      | —       | The prompt to complete          |
| `model`             | string  | Yes      | —       | Model ID                        |
| `max_tokens`        | integer | No       | 100     | Maximum tokens (1-4096)         |
| `temperature`       | number  | No       | 0.7     | Sampling temperature (0.0-2.0)  |
| `top_p`             | number  | No       | 0.9     | Nucleus sampling (0.0-1.0)      |
| `top_k`             | integer | No       | 50      | Top-k sampling                  |
| `frequency_penalty` | number  | No       | 0.0     | Frequency penalty (-2.0 to 2.0) |
| `presence_penalty`  | number  | No       | 0.0     | Presence penalty (-2.0 to 2.0)  |
| `stop`              | array   | No       | —       | Up to 4 stop sequences          |
| `user`              | string  | No       | —       | End-user identifier             |

### Example Request

<CodeGroup>
  ```bash cURL theme={null}
  # Edge endpoints accept your pg_* API key as a bearer token — see Authentication
  curl -X POST https://api.yto-01.edge.polargrid.ai/v1/completions \
    -H "Authorization: Bearer pg_your_api_key" \
    -H "Content-Type: application/json" \
    -d '{
      "prompt": "Once upon a time",
      "model": "qwen-3.5-27b",
      "max_tokens": 100,
      "temperature": 0.8
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await client.completion({
    prompt: 'Once upon a time',
    model: 'qwen-3.5-27b',
    maxTokens: 100,
    temperature: 0.8,
  });

  console.log(response.choices[0].text);
  ```

  ```python Python theme={null}
  response = await client.completion({
      "prompt": "Once upon a time",
      "model": "qwen-3.5-27b",
      "max_tokens": 100,
      "temperature": 0.8,
  })

  print(response.choices[0].text)
  ```
</CodeGroup>

### Response

```json theme={null}
{
  "id": "cmpl-abc123",
  "object": "text_completion",
  "created": 1234567890,
  "model": "qwen-3.5-27b",
  "choices": [
    {
      "text": " in a land far away, there lived a young princess...",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 4,
    "completion_tokens": 100,
    "total_tokens": 104
  }
}
```

## Streaming

Enable streaming for real-time token generation:

<CodeGroup>
  ```javascript JavaScript theme={null}
  for await (const chunk of client.completionStream({
    prompt: 'Once upon a time',
    model: 'qwen-3.5-27b',
  })) {
    process.stdout.write(chunk.choices[0].text);
  }
  ```

  ```python Python theme={null}
  async for chunk in client.completion_stream({
      "prompt": "Once upon a time",
      "model": "qwen-3.5-27b",
  }):
      print(chunk.choices[0].text, end="", flush=True)
  ```
</CodeGroup>

## Legacy Generate Method

The SDKs also provide a `generate()` method for backward compatibility. It wraps `chatCompletion()` internally:

```javascript theme={null}
const response = await client.generate({
  model: 'qwen-3.5-27b',
  prompt: 'Hello, how are you?',
  maxTokens: 100,
});

console.log(response.content);
console.log(`Processing time: ${response.processingTimeMs}ms`);
```
