Arguments

  1. apiKey
    string
    required
    The API key for the Makeswift site.
  2. options
    object
    required
    Options for site version and locale.

Examples

App Router

src/app/api/makeswift/[...makeswift]/route.ts
import { MakeswiftApiHandler } from "@makeswift/runtime/next/server";
import { strict } from "assert";

import { runtime } from "@/makeswift/runtime";

// make custom components' data available for introspection
import "@/makeswift/components";

strict(
  process.env.MAKESWIFT_SITE_API_KEY,
  "MAKESWIFT_SITE_API_KEY is required"
);

const handler = MakeswiftApiHandler(process.env.MAKESWIFT_SITE_API_KEY, {
  runtime,
});

export { handler as GET, handler as POST, handler as OPTIONS };

Pages router

src/pages/api/makeswift/[...makeswift].ts
import { MakeswiftApiHandler } from "@makeswift/runtime/next/server";
import { strict } from "assert";

import { runtime } from "@/makeswift/runtime";

// make custom components' data available for introspection
import "@/makeswift/components";

strict(
  process.env.MAKESWIFT_SITE_API_KEY,
  "MAKESWIFT_SITE_API_KEY is required"
);

export default MakeswiftApiHandler(process.env.MAKESWIFT_SITE_API_KEY, {
  runtime,
});

Adding fonts

The following example adds Spline Sans and Spline Sans Mono Google Fonts to the site using next/font and adds them to the MakeswiftApiHandler. For more information on adding fonts to your Next.js app, see our Adding Fonts guide.
We recommend using variable fonts as they reduce the number of font files requested.
import { MakeswiftApiHandler } from "@makeswift/runtime/next/server";
import { strict } from "assert";

import { runtime } from "@/makeswift/runtime";

strict(
  process.env.MAKESWIFT_SITE_API_KEY,
  "MAKESWIFT_SITE_API_KEY is required"
);

export default MakeswiftApiHandler(process.env.MAKESWIFT_SITE_API_KEY, {
  runtime,
  getFonts() {
    return [
      {
        family: "Spline Sans",
        variants: [
          {
            weight: "300",
            style: "normal",
          },
          {
            weight: "400",
            style: "normal",
          },
          {
            weight: "500",
            style: "normal",
          },
        ],
      },
      {
        family: "Spline Sans Mono",
        variants: [
          {
            weight: "500",
            style: "normal",
          },
        ],
      },
    ];
  },
});

Using onPublish event

import { MakeswiftApiHandler } from "@makeswift/runtime/next/server";
import { strict } from "assert";

import { runtime } from "@/makeswift/runtime";

strict(
  process.env.MAKESWIFT_SITE_API_KEY,
  "MAKESWIFT_SITE_API_KEY is required"
);

const handler = MakeswiftApiHandler(process.env.MAKESWIFT_SITE_API_KEY, {
  runtime,
  events: {
    onPublish() {
      console.log(`Published content`);
    },
  },
});

export { handler as GET, handler as POST, handler as OPTIONS };