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

# Generate text from a prompt

> Generate text using the text generation endpoint. Compare text generation vs chat completion and see API examples.

<Warning>
  **Deprecation notice**: The following models are deprecated and will be removed on **July 13, 2026**: `palmyra-x-003-instruct`, `palmyra-vision`, `palmyra-med`, `palmyra-fin`, and `palmyra-creative`.

  **Migration path**: Use [`palmyra-x5`](/home/models#palmyra-x5) as the replacement for all deprecated models. Palmyra X5 supports a 1M-token context window and covers general-purpose, financial, medical, and creative use cases. For vision workloads, use [chat with images](/home/chat-with-images) with Palmyra X5 instead of `palmyra-vision`. See the [deprecation policy](/home/models#deprecation-policy) for more information.
</Warning>

You can use the [text generation endpoint](/api-reference/completion-api/text-generation) to generate text with an LLM.

<Note>
  You need an API key to access the Writer API. Get an API key by following the steps in the [API quickstart](/home/quickstart).

  We recommend setting the API key as an environment variable in a `.env` file with the name `WRITER_API_KEY`.
</Note>

## Text generation vs. chat completion

The text generation endpoint is appropriate when you need to generate a single text response based on a given prompt, or when you want to ask a specific LLM a question.

The [chat completion endpoint](/home/chat-completion) can generate single messages, or create more complex conversations between a user and an general-purpose LLM. Additionally, the chat completion endpoint offers [tool calling](/home/chat-completion), which you can use to access domain-specific LLMs, Knowledge Graphs, and custom functions.

## Endpoint overview

**URL:** `POST https://api.writer.com/v1/completions`

<Warning>
  Using the `/completions` endpoint results in charges for **model usage**. See the [pricing page](/home/pricing) for more information.
</Warning>

<CodeGroup>
  ```bash cURL theme={null}
  curl --location 'https://api.writer.com/v1/completions' \
  --header 'Content-Type: application/json' \
  --header "Authorization: Bearer $WRITER_API_KEY" \
  --data '{
    "model": "palmyra-x-003-instruct",
    "prompt": "Tell me a story",
    "max_tokens": 1000,
    "temperature": 0.7,
    "stream": true
  }'
  ```

  ```python Python theme={null}
  from writerai import Writer

  # Initialize the client. If you don't pass the `api_key` parameter,
  # the client looks for the `WRITER_API_KEY` environment variable.
  client = Writer()

  text_generation = client.completions.create(
    model="palmyra-x-003-instruct",
    prompt="Tell me a story",
    max_tokens=1000,
    temperature=0.7,
    stream=True
  )

  for chunk in text_generation:
      print(chunk.value, end="", flush=True)
  ```

  ```javascript JavaScript theme={null}
  import { Writer } from 'writer-sdk';

  // Initialize the client. If you don't pass the `apiKey` parameter,
  // the client looks for the `WRITER_API_KEY` environment variable.
  const client = new Writer();

  const text_generation = await client.completions.create({
    model: 'palmyra-x-003-instruct',
    prompt: 'Tell me a story',
    max_tokens: 1000,
    temperature: 0.7,
    stream: true 
  });

  for await (const chunk of text_generation) {
      process.stdout.write(chunk.value);
  }
  ```
</CodeGroup>

### Request body

Below are the required and commonly used optional parameters for the text generation endpoint.

| Parameter     | Type    | Description                                                                                                                                                                      |
| ------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model`       | string  | **Required**. The [ID of the model](/home/models) to use for text generation.                                                                                                    |
| `prompt`      | string  | **Required**. The prompt to generate text from.                                                                                                                                  |
| `max_tokens`  | int     | The maximum number of tokens to generate for the response. Defaults to `100`.                                                                                                    |
| `temperature` | float   | Temperature influences the randomness in generated text. Defaults to `1`. Increase the value for more creative responses, and decrease the value for more predictable responses. |
| `stream`      | Boolean | A Boolean value that indicates whether to stream the response. Defaults to `false`.                                                                                              |

See the full list of available parameters in the [text generation endpoint reference](/api-reference/completion-api/text-generation).

### Response parameters

#### Non-streaming response

If you set the `stream` parameter to `false`, the response is a single JSON object with the following parameters:

| Parameter              | Type   | Description                                                                                                                            |
| ---------------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------- |
| `model`                | string | The ID of the model used to generate the response.                                                                                     |
| `choices`              | array  | An array of choices objects.                                                                                                           |
| `choices[0].text`      | string | The generated text.                                                                                                                    |
| `choices[0].log_probs` | object | The [log probabilities](/api-reference/completion-api/text-generation#response-choices-log-probs) of the tokens in the generated text. |

```json theme={null}
{
  "choices": [
    {
      "text": "Camping Gear: The Ultimate Guide\n\nCamping is a great way to get outdoors and enjoy nature",
      "log_probs": null
    }
  ],
  "model": "palmyra-x-003-instruct"
}
```

#### Streaming response

If you set the `stream` parameter to `true`, the response is delivered as [server-sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) with the following parameters:

| Parameter | Description               |
| --------- | ------------------------- |
| `value`   | The content of the chunk. |

```json theme={null}
data: {"value":"Camping Gear: The Ultimate Guide\n\nCamping is a great way to get outdoors and enjoy nature"}
```

## Example request to a specific LLM

The examples below generate a single message from the `palmyra-med` model, using the prompt "How can I treat a cold?"

### Streaming response

The text generation endpoint supports streaming responses. The response comes in chunks until the entire response finishes.

Streaming responses are useful when you want to display the generated text in real-time, or when you want to stream the response to a client, rather than waiting for the entire response to finish.

<CodeGroup>
  ```bash cURL theme={null}
  curl --location 'https://api.writer.com/v1/completions' \
  --header 'Content-Type: application/json' \
  --header "Authorization: Bearer $WRITER_API_KEY" \
  --data '{
    "model": "palmyra-med",
    "prompt": "How can I treat a cold?",
    "stream": true
  }'
  ```

  ```python Python theme={null}
  from writerai import Writer

  # Initialize the client. If you don't pass the `api_key` parameter,
  # the client looks for the `WRITER_API_KEY` environment variable.
  client = Writer()

  text_generation = client.completions.create(
    model="palmyra-med",
    prompt="How can I treat a cold?",
    stream=True
  )

  for chunk in text_generation:
      print(chunk.value, end="", flush=True)
  ```

  ```javascript JavaScript theme={null}
  import { Writer } from 'writer-sdk';

  // Initialize the client. If you don't pass the `apiKey` parameter,
  // the client looks for the `WRITER_API_KEY` environment variable.
  const client = new Writer();

  const text_generation = await client.completions.create({
    model: 'palmyra-med',
    prompt: 'How can I treat a cold?',
    stream: true 
  });

  for await (const chunk of text_generation) {
      process.stdout.write(chunk.value);
  }
  ```
</CodeGroup>

### Non-streaming response

For non-streaming responses, the response returns as a single JSON object after the entire response is complete. The text is in the `choices[0].text` field.

<CodeGroup>
  ```bash cURL theme={null}
  curl --location 'https://api.writer.com/v1/completions' \
  --header 'Content-Type: application/json' \
  --header "Authorization: Bearer $WRITER_API_KEY" \
  --data '{
    "model": "palmyra-med",
    "prompt": "How can I treat a cold?",
    "stream": false
  }'
  ```

  ```python Python theme={null}
  from writerai import Writer

  # Initialize the client. If you don't pass the `api_key` parameter,
  # the client looks for the `WRITER_API_KEY` environment variable.
  client = Writer()

  text_generation = client.completions.create(
    model="palmyra-med",
    prompt="How can I treat a cold?",
    stream=False
  )

  print(text_generation.choices[0].text)
  ```

  ```javascript JavaScript theme={null}
  import { Writer } from 'writer-sdk';

  // Initialize the client. If you don't pass the `apiKey` parameter,
  // the client looks for the `WRITER_API_KEY` environment variable.
  const client = new Writer();

  const text_generation = await client.completions.create({
    model: 'palmyra-med',
    prompt: 'How can I treat a cold?',
    stream: false
  });

  console.log(text_generation.choices[0].text);
  ```
</CodeGroup>

## Next steps

Now that you've generated text, try out the following:

* Create a chat with an AI assistant using the [chat completion endpoint](/home/chat-completion)
* Learn more about the [tool calling](/home/chat-completion#tool-calling) feature of the chat completion endpoint
