This guide helps you get started with the Writer SDKs. Follow these steps to install the SDKs and perform basic operations.

Prerequisites

  • Python 3.7+
  • pip
  • A Writer API key

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.

Install the SDK

Open your terminal or command prompt and install the Writer SDK:

pip install writer-sdk

Initialize the client

To initialize the client, import the Writer SDK and create an instance of the Writer class.

We recommend setting your API key in an environment variable called WRITER_API_KEY. When you initialize the Writer client, the client looks for the WRITER_API_KEY environment variable automatically. You can also pass your API key directly to the client.

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()

Make a chat completion request

Once you’ve initialized the client, you can make a request to the Writer API. The following example shows how to make a chat completion request to generate a poem:

from writerai import Writer

client = Writer()

response = client.chat.chat(
    messages=[{ "content": "Write a short poem about Python", "role": 'user' }],
    model="palmyra-x-004"
)

print(response.choices[0].message.content)

Next steps

Now that you’re set up with the SDKs, start building with chat completions or text generation.

You can also use the API reference to learn more detailed information about available endpoints.

Was this page helpful?