# Connect Next.js publishing to Inkie

Inkie sends a signed request to your Next.js server after the owner separately
approves finished content. Your server stores and renders the article. Saving
the connection sends only a signed ping; it never publishes a test article.

Your site never calls Inkie or reads Inkie's database. It keeps its own copy of
each article, so the blog keeps working if Inkie is unavailable.

## Developer handoff

Read the installed Next.js Route Handler and caching documentation first. This
example follows the App Router Web Request/Response APIs and Node runtime in
Next.js 16. Adapt the `/blog` path to the site's existing conventions everywhere.

1. Download `/guides/nextjs-publishing-receiver.ts` from Inkie and copy it to
   `lib/inkie-receiver.ts`. It requires Zod 4 and Node's built-in crypto module.
2. Generate a random signing secret of at least 32 UTF-8 bytes (32 ASCII characters). Save it as
   `INKIE_WEBHOOK_SECRET` on the receiving server and in this Brand's Inkie
   connection. Never use a `NEXT_PUBLIC_` variable or commit the secret.
3. Implement `InkieArticleStore` against the site's durable database. Use unique
   constraints on Inkie article ID and public slug, and perform identity checks
   plus insertion in one transaction. Do not use process memory or local disk
   in a serverless deployment.

The store's `save(article, contentHash)` must implement these rules atomically:

- If `articleId` exists, require the saved content hash to match. If an
  `existingExternalId` was supplied, it must identify that same row. Return the
  original ID and slug. A retry must never create a second article.
- If `articleId` is missing and `createIfMissing` is false or an
  `existingExternalId` is supplied, return `missing` without inserting.
- If the slug belongs to another article, return `conflict`. Do not overwrite
  manually authored content or an article delivered by another Brand.
- Otherwise insert the article, hash, and stable external ID. Return `saved`.
  Concurrent unique-constraint conflicts must be re-read and classified by the
  same rules. This sample treats a delivered article as immutable; handle later
  editorial changes through your site's explicit revision workflow.

Example PostgreSQL table (adapt migration naming to the receiving project):

```sql
create table inkie_articles (
  id uuid primary key default gen_random_uuid(),
  inkie_article_id text not null unique,
  slug text not null unique,
  content_hash text not null,
  title text not null,
  html text not null,
  meta_title text,
  meta_description text,
  hero_image_url text,
  hero_image_alt text,
  published_at timestamptz not null
);
```

Create `app/api/inkie/route.ts` after implementing that store:

```ts
import { revalidatePath } from "next/cache";
import { createInkieReceiver } from "@/lib/inkie-receiver";
import { inkieArticleStore } from "@/lib/inkie-article-store";

export const runtime = "nodejs";
export const POST = createInkieReceiver({
  secret: process.env.INKIE_WEBHOOK_SECRET ?? "",
  previousSecret: process.env.INKIE_WEBHOOK_SECRET_PREVIOUS,
  origin: "https://your-site.example",
  store: inkieArticleStore,
  refresh: path => revalidatePath(path),
});
```

The receiver verifies HMAC-SHA256 over `webhookId.timestamp.rawBody`, checks a
five-minute timestamp window in both directions, limits the body to 2 MB,
validates the article, and handles ping without storage. A timestamp check limits
replay age; durable article identity prevents a valid repeated request from
creating duplicate content. Inkie sends only the signature, never the secret.

## Changing the signing secret

The receiver accepts a second, previous secret so publishing keeps working while
the secret changes:

1. In Inkie, open Connect and verify, choose Change settings, then Replace with
   a new secret. Copy it. Don't save yet.
2. Deploy with the new secret as `INKIE_WEBHOOK_SECRET` and the old one as
   `INKIE_WEBHOOK_SECRET_PREVIOUS`. The receiver accepts either.
3. Save and verify in Inkie. Inkie signs with the new secret from then on.
4. Remove `INKIE_WEBHOOK_SECRET_PREVIOUS` and deploy again.

If the old secret leaked, skip the previous secret. Deploy the new one alone,
then save it in Inkie straight away. Until you do, Inkie's check fails and
nothing publishes.

## Public rendering and verification

Render stored articles in `app/blog/[slug]/page.tsx`. In Next.js 16, await the
`params` promise before reading `slug`; return `notFound()` when absent. Use a
server database lookup, render the full article in initial HTML, and sanitize
HTML with an established allowlist before using `dangerouslySetInnerHTML`.
Preserve useful links and semantic headings. Do not render scripts or event
attributes. Provide metadata and a canonical URL from your own public origin.

The HTML excludes the article title and its hero image. Render the title as the
page heading, show `hero_image_url` above the article with `hero_image_alt` as
its alt text, and use it as the `og:image`. Keep `figure`, `img`, `figcaption`,
`aside` and YouTube `iframe` elements in your sanitizer allowlist: articles
include inline images, a key-takeaways box and an embedded video.

List published articles in `/blog` and the sitemap. Both the article and index
must be publicly readable without cookies or authentication. Intentional private
content must remain private and cannot be this publishing destination.

The example invalidates the article, blog index, and sitemap after saving. From
a Route Handler, `revalidatePath` refreshes a path on its next visit. If other
pages share cached article queries, also invalidate their tags using the API
documented for your installed Next.js version. A successful webhook response
means storage succeeded; Inkie separately fetches the returned URL to verify
public implementation. It does not promise later LLM citations.

## Owner setup

Open **Settings → Integrations → Next.js** in Inkie. The guide shows these
steps with the code to copy, and generates the signing secret. Enter the
public HTTPS endpoint (for example `https://your-site.example/api/inkie`) and
that secret under Connect and verify, then choose Save and verify. Return to
the draft, then read and approve its current revision before publication.
Copying the article for manual publishing remains available.

## Staging acceptance checks

Use an explicitly approved test article and staging destination. Check an invalid
signature, stale and future timestamp, signed ping, duplicate delivery, timeout
after insertion, conflicting slug, mismatched external ID, missing article
with `createIfMissing=false`, and a delivery signed with the previous secret
while the secret changes. The same article must retain one ID and URL after
retry. Confirm public HTML, metadata, internal links, indexing directives, and
sitemap inclusion. Also test changed destinations and edited drafts in Inkie;
both must require the appropriate current approval. Do not send test content to
production merely to validate the connection.

The downloadable receiver has local automated contract tests. This guide is
not evidence that your deployment, database store, or public rendering works.
