> For the complete documentation index, see [llms.txt](https://developers.copyfactory.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.copyfactory.io/api/generations/generate-text.md).

# Generate text

To generate text you need a [premise ID](/api/premises.md) and a dictionary containing **keys** that are the the **required fields** for the premise fields and **values** that contain the actual **data**.&#x20;

Here is a basic example for a **sentence** premise where the required datapoints would have been \[' first\_name'*, '*&#x70;erson\_description'].

```
{
	"first_name": "Mark",
	"person_description": "This is my description of someone..."
}
```

If you do not know the required fields for your premise ID use the [get premise endpoint](/api/premises/get-a-premise.md).

{% hint style="warning" %}
If you receive an empty sentence back this is likely due to an error in your variables. Make sure to check the required fields and try again. All generations will contain meta data on the request.
{% endhint %}

## Generate text for personalizing contacts or for generating entire emails.

<mark style="color:green;">`POST`</mark> `https://app.copyfactory.io/api/v2/generate/`

#### Headers

| Name                                            | Type   | Description              |
| ----------------------------------------------- | ------ | ------------------------ |
| Authorization<mark style="color:red;">\*</mark> | string | Your Copyfactory API key |

#### Request Body

| Name                                          | Type   | Description                                                                                                  |
| --------------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------ |
| premise\_id<mark style="color:red;">\*</mark> | int    | The ID of the sentence premise you wish to use.                                                              |
| variables<mark style="color:red;">\*</mark>   | object | A dictionary with keys containing the required data fields and values which contain the actual contact data. |

{% tabs %}
{% tab title="200: OK " %}

```javascript
{
	"status": "success",
	"content": "This is the generated sentence based on the prospect data.",
	"meta_data": {
		"sentence_premise": {
			"id": 20,
			"order": 1,
			"is_active": False,
			"required_variables": ["first_name", "company_description"],
			"sentence_premise_name": "Company - Edited",
			"profile_name": "Copyfactory"
		},
		"variables": {
			"first_name": "Mark",
			"company_description": "This is my company description"
		}
	}
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Shell" %}

```
curl --location --request POST 'https://app.copyfactory.io/api/v2/generate/' \
--header 'Authorization: API-KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
            "premise_id": PREMISE_ID,
            "variables": {"field_name":"field data"}
}'
```

{% endtab %}

{% tab title="Python" %}

```
import requests
import json

url = "https://app.copyfactory.io/api/v2/generate/"

payload = json.dumps({
  "premise_id": PREMISE_ID,
  "variables": {
    "field_name": "field data"
  }
})

headers = {
  'Authorization': 'API-KEY',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers.copyfactory.io/api/generations/generate-text.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
