Skip to main content
You can use the text generation endpoint to generate text with an LLM.
You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.We recommend setting the API key as an environment variable in a .env file with the name WRITER_API_KEY.

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 can generate single messages, or create more complex conversations between a user and a general-purpose LLM. Additionally, the chat completion endpoint offers tool calling, which you can use to access other LLMs, Knowledge Graphs, and custom functions.

Endpoint overview

URL: POST https://api.writer.com/v1/completions
Using the /completions endpoint results in charges for model usage. See the pricing page for more information.

Request body

Below are the required and commonly used optional parameters for the text generation endpoint. See the full list of available parameters in the text generation endpoint reference.

Response parameters

Non-streaming response

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

Streaming response

If you set the stream parameter to true, the response is delivered as server-sent events with the following parameters:

Generate streaming and non-streaming responses

The examples below generate a single message from the palmyra-x5 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.

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.

Next steps

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