crontent
Integration·Gatsby·intermediate

AI content for Gatsby

AI content for Gatsby: source Crontent posts into your GraphQL layer from a verified webhook as unpublished drafts.

How-to: Integrate Crontent with Gatsby. Official docs: Gatsby. Shared API contract: Publishing API and SDK.

Verify Crontent, fetch the post, store an unpublished draft in the CMS or filesystem your site sources from, then rebuild so GraphQL picks it up.

Field mapping

Crontent fieldGatsby node field
titletitle
seo_titleseoTitle for SEO component
slugslug, used in createPages
excerptexcerpt
body_mdxbody (process with gatsby-plugin-mdx)
body_htmlhtml if not using MDX
tagstags[]
hero_image_urlfeaturedImage remote file node
canonical_urlcanonicalUrl
sources[]sources JSON

Webhook handler

Use Gatsby Functions on Gatsby Cloud or Netlify:

ts
// src/api/crontent-webhook.ts
import { Crontent } from "@crontent/sdk";
import { writeFile, mkdir } from "node:fs/promises";
import { join } from "node:path";

const crontent = new Crontent({ apiKey: process.env.CRONTENT_API_KEY! });

export default async function handler(req, res) {
  if (req.method !== "POST") return res.status(405).end();
  const rawBody = req.rawBody ?? JSON.stringify(req.body);
  const valid = await crontent.webhooks.verify({
    rawBody,
    signature: req.headers["x-crontent-signature"],
    timestamp: req.headers["x-crontent-timestamp"],
    secret: process.env.CRONTENT_WEBHOOK_SECRET!,
  });
  if (!valid) return res.status(401).send("invalid");

  const { post_id } = JSON.parse(rawBody);
  const post = await crontent.posts.get(post_id);
  const dir = join(process.cwd(), "content/crontent");
  await mkdir(dir, { recursive: true });
  await writeFile(
    join(dir, `${post.slug}.json`),
    JSON.stringify({ ...post, published: false }, null, 2)
  );

  // Trigger rebuild via Gatsby Cloud webhook or your CI
  res.status(200).send("ok");
}

In gatsby-node.js, source JSON files with createNode and filter published !== true from page creation.

CMS alternative

If you already use Contentful or Sanity, POST unpublished entries from the webhook instead of local JSON. Gatsby's source plugins pick them up on the next build.

Backfill

Paginate crontent.posts.list and write missing JSON or CMS entries. Trigger one rebuild after import.

Further reading