Knock keeps the rendered content of every message it sends, such as an email's subject line and HTML body, only during your account's data retention window: 30 days, or 90 days on the Enterprise plan. Once a message ages out of that window, the get message content endpoint stops returning it, and you won't be able to view it in the Knock dashboard.
To preserve content for longer or to keep it in a system you control, you can archive each message as Knock sends it. Common reasons include:
- Compliance and audit records. Organizations in regulated industries, such as healthcare, financial services, and legal, often must show exactly what they sent a user, such as a terms of service update, a disclosure, or a security alert.
- Retention beyond your plan's window. Your archive keeps content for as long as your own retention policy requires.
- Customer support. A support team can look up the exact notification a customer received when the customer asks about it, including after Knock's copy has expired.
- Content analysis. You can review or analyze rendered copy alongside the rest of your data.
This tutorial builds an archiver that listens for sent messages with an outbound webhook, fetches their rendered content from the Knock API, and writes a record for each one to storage you choose.
- Knock sends a
message.sent webhook to your endpoint when it hands a message to the channel's provider (or, for Knock's in-app channels, when it creates the message).
- Your endpoint verifies the webhook's signature, checks the message's
source (the workflow, broadcast, or guide that produced it) to decide whether to archive it, and adds matching events to a queue.
- A worker takes each event from the queue, calls the get message content endpoint, and writes the message metadata and rendered content to your storage, keyed by message ID.
- A scheduled job lists recent messages through the API and archives any that the webhook missed.
The queue between your endpoint and the worker keeps your endpoint fast and keeps an event from being lost if archiving fails after your endpoint responds to Knock.
- A Knock account with at least one workflow that sends the messages you want to archive.
- A secret API key for the environment you'll archive from.
- A server with a publicly accessible HTTPS endpoint to receive webhooks.
- Durable storage for the archive, such as an object storage bucket or a database table.
- A queue or background job system, such as Amazon SQS, Google Cloud Pub/Sub, or a jobs table in your database.
Complete the following steps to build an archiver that stores the rendered content of each message that matches your archiving rule.
Choose which messages to archive
Your endpoint receives a message.sent event for every message in the webhook's environment, so it needs a rule for which ones to keep. The event's data.source object identifies where the message came from:
source.categories lists the categories assigned to a workflow or broadcast. Use a category, such as archive, when several workflows or broadcasts share the same archiving requirement. Adding the category to a new workflow or broadcast then opts it in without a code change.
source.key is the workflow, broadcast, or guide key. You can match on it when you want to archive a short, fixed list of message sources. It's also the only way to match guide messages.
Create the webhook
Webhooks are configured per environment. Create one in each environment you want to archive from and store each webhook's signing secret with the matching environment's API key.
- Go to Platform > Webhooks and click Create webhook.
- Enter your endpoint URL, select the
message.sent event, and save the webhook.
- Open the webhook, copy the signing secret from its Overview tab, and store it with your server's secrets.
Knock emits message.sent for every channel type, including Knock's in-app feed and guide channels. The message.delivered event isn't a good option on its own for archival purposes, because certain channels (like push and chat) never reach the delivered status due to provider limitations.
If your records must also show a message's delivery or engagement outcome, subscribe the same webhook to additional message events and store them alongside the archived record. On the Enterprise plan, the warehouse export can sync these events to your data warehouse and you can join its rows to your archive on the message ID.
Handle the webhook
Your endpoint should do three things with each request:
- Verify the signature. Follow the webhook signature verification steps. Respond with a
400 status code to any request that fails the check.
- Apply your archiving rule. Check the webhook's
data.source.categories or data.source.key against the rule you chose. For events that you don't archive, respond with a 200 right away.
- Queue the event, then respond. Add any event that matches your archive rules to your queue, and respond with a
200 once the queue accepts it. If the queue is unavailable, respond with a 5xx status so that Knock retries the webhook.
The following message.sent event is a truncated example for an email from a workflow with the archive category. See the Message object schema for the full list of fields.
Archive the message content
For each queued event, your worker calls the get message content endpoint with the message ID from data.id. The response contains the message's rendered content:
The shape of data depends on the channel type, and its __typename tells you which one you have:
__typename | Channel type | Fields |
|---|
MessageEmailContent | Email | to, from, reply_to, cc, bcc, subject_line, html_body, text_body |
MessageSmsContent | SMS | to, body |
MessagePushContent | Push | token, title, body, data |
MessageChatContent | Chat | template, metadata |
MessageInAppFeedContent | In-app feed | blocks |
Write one record per message, combining the message metadata from the webhook event with the rendered content:
| Field | Source | Why keep it |
|---|
id | The event's data | Identifies the message, and serves as the record's key. |
source | The event's data | The key, categories, and version of the workflow, broadcast, or guide that produced the message, plus run IDs. |
channel | The event's data | The channel type and provider the message went through. |
recipient | The event's data | The ID of the user or object that received the message. |
recipient_snapshot | The event's data | The email address or chat destination at send time. The recipient's data in Knock can change after the send. |
inserted_at | The event's data | When Knock created the message. |
data | Content response | The rendered content. |
If the API call or the storage write fails, let your queue retry the job.
Catch missed events
Knock retries a failed webhook a handful of times over a few hours. If your endpoint is down for longer than that, you may miss archiving some messages. Run a scheduled job that lists recent messages through the list messages endpoint, filtered to the messages your archiving rule covers:
workflow_categories set to your archiving category. To match on workflow keys instead, pass source with one key per request.
status set to sent, delivered, and bounced, the statuses a message reaches only after Knock sends it.
inserted_at.gte set to a time before the previous run, so each run overlaps the last. A daily job can look back two days.
For each message the endpoint returns, check whether your storage already has a record for its ID. If it doesn't, archive the message the same way your worker does.
- Archive within the retention window. The get message content endpoint returns content only while the message is within your account's retention window.
- Rate limits. The get message content endpoint has a rate limit of 200 requests per second per environment. At higher volume, have the worker collect message IDs and fetch them together with the batch get message content endpoint.
- Attachments. The content response doesn't include email attachments. If your records must include them, archive the attachment files from your own system when you trigger the workflow.
- Sensitive data. The archive holds rendered content and recipient contact details. Be sure to apply any necessary access controls, encryption, and deletion policies.
Frequently asked questions
#