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

# Content generation

> Content generation steps and endpoints

## Overview

After prospect is created and enriched, we can start generating content for them. Using predefined templates for the content generation, we can create personalized messages for the prospects.

<Note>
  You can request content generation right after the prospect is created, it will wait for the enrichment process to complete before starting the content generation.
</Note>

The content generation process is asynchronous and can take a few seconds to complete. The status will change from `waiting` then `generating` and finally `completed`.

## Content generation process

The content generation process is triggered by creating a new content generation request. The system will use the prospect data to personalize the content and generate the message.

You can get content using two methods:

* **Polling**: You can check the status of the content generation request using the `/content/{id}` endpoint.
* **Webhooks**: You can set up a webhook to receive the content when it is ready.

## Webhooks

To get a webhook, you need to provide a callback URL in the `callback` field when creating the content generation request.
The system will send a POST request to this URL with the content meta information when the status changes to `completed`.

<Note>
  It will not contain the content itself, but only the metadata about the content.
</Note>

Payload will look like this:

```json theme={null}
{
  "id": "string",
  "pattern": "string",
  "status": "completed",
  "prospect": "string",
  "template": "string",
  "language": "string",
  "user": "string"
}

```

Read more about available fields in the [content creation API](/api-reference/content/create).

## Webhook signing

<Note>
  On your request, we can enable webhook signing to ensure the integrity of the data sent to your callback URL.
</Note>

When enabled, each webhook will include a `X-Webhook-Signature` header containing a HMAC SHA256 signature of the payload. You can verify this signature using your webhook secret key.

We are signing the payload using the following method:

1. Create a string from the JSON payload.
2. Create a HMAC SHA256 hash of the string using your webhook secret key.
3. Next, we encode the hash as a hexadecimal string and prepend it with `sha256=`.
4. The resulting string is the value of the `X-Webhook-Signature` header.

Here is an example of how to verify the signature in Node.js:

```javascript theme={null}
// Import the crypto module
const crypto = require('crypto');

// Assuming you have the request object `req` from your Express.js route handler
const payload = req.body;
const secret = "your-webhook-secret";

// Generate the signature
const signature = `sha256=${crypto.createHmac('sha256', secret).update(payload).digest('hex')}`;
```

## Content data model

In our content generation we are using the following model for content:

```json theme={null}
{
  "id": "string",
  "user": "string",
  "prospect": "string",
  "template": "string",
  "status": "waiting" | "generating" | "completed",
  "language": "string",
  "analytics": [
    {
      "id": "string",
      "selector": "string",
      "type": "view" | "click" | "duration"
    }
  ],
  "content": [
    {
      "id": "string",
      "type": "text" | "image",
      "payload": "string"
    }
  ],
  "createdAt": "datetime",
  "updatedAt": "datetime"
}
```

The content field is an array of content blocks that can be text or image. The exact structure depends on the template you are requesting the content for.
