Specify docs

Next.js

Add Specify to a Next.js app.

Set up with the wizard

npx @specify-sh/wizard --publisher

The wizard runs through the coding agent you already use (Claude Code, Codex etc.). It inspects your codebase, proposes an integration plan and implements it as a reviewable diff.

Prefer to do it by hand? The manual steps below are the same setup.

Manual setup

Runnable example

nextjs-typescript is this guide as a working App Router app.

Next.js can serve ads two ways:

Where you serveImportIdentity comes from
Client component@specify-sh/publisher-sdkThe visitor's cookie, plus wallets you pass
Server component, route handler, server action (Not recommended)@specify-sh/publisher-sdk/serverWallets you pass

Serve from client components where you can. Only the browser has the visitor's Specify cookie, so only a client component can fill a placement for a visitor with no wallet connected.

1. Install

npm i @specify-sh/publisher-sdk

2. Set your keys

You will need a publisher key. See Publisher keys.

The variable needs the NEXT_PUBLIC_ prefix, which is fine: publisher keys are public by design.

.env.local
NEXT_PUBLIC_SPECIFY_PUBLISHER_KEY=spk_your_key_here

3. Create the client

Create the client in its own module and import it where you need it.

lib/specify.ts
import Specify from '@specify-sh/publisher-sdk';

export const specify = new Specify({
  publisherKey: process.env.NEXT_PUBLIC_SPECIFY_PUBLISHER_KEY!
});

Wire your consent platform to the SDK, high in the component tree. The SDK never stores consent, so pass it on every page load.

components/ConsentBridge.tsx
'use client';

import { useEffect } from 'react';
import { specify } from '@/lib/specify';

// Adapt this code to your cookie consent management tool
export function ConsentBridge() {
  useEffect(() => {
    specify.setCookieConsent(cmp.getConsent().targetedAdvertising);

    return cmp.onConsentChange((consent) => {
      specify.setCookieConsent(consent.targetedAdvertising);
    });
  }, []);

  return null;
}

With consent, Specify recognises visitors across its network, so a placement can fill even for a visitor who hasn't connected a wallet to your app.

5. Register connected wallets

If your app has a wallet connection flow, notify the SDK when a wallet is connected. Every later serve() includes it, so components deeper in the tree do not need the address passed down.

components/WalletBridge.tsx
'use client';

import { useEffect } from 'react';
import { useAccount } from 'wagmi';
import { specify } from '@/lib/specify';

export function WalletBridge() {
  const { address } = useAccount();

  useEffect(() => {
    if (address) specify.identify(address);
  }, [address]);

  return null;
}

6. Serve an ad

useSpecifyAd() serves on mount, tries again when the identity improves, and keeps the first ad it gets.

components/AdSlot.tsx
'use client';

import { ImageFormat } from '@specify-sh/publisher-sdk';
import { useSpecifyAd } from '@specify-sh/publisher-sdk/react';
import { specify } from '@/lib/specify';

export function AdSlot() {
  const ad = useSpecifyAd({
    specify,
    imageFormat: ImageFormat.LANDSCAPE,
    adUnitId: 'sidebar'
  });

  if (!ad) return null;

  return (
    <a href={ad.ctaUrl} target="_blank" rel="noopener noreferrer">
      {ad.imageUrl && <img src={ad.imageUrl} alt="" />}
      <h3>{ad.headline}</h3>
      <p>{ad.content}</p>
      <span>{ad.ctaLabel}</span>
    </a>
  );
}

Available image formats and the fields returned in the ad can be found in the Browser SDK reference.

Check it works

With a development key you get rotating sample ads, and nothing reaches your reporting.

  1. Load a page with a slot and confirm a sample ad renders, labelled as sponsored. See Label every ad.
  2. Click the call to action and confirm it opens ctaUrl.
  3. Check what happens when serve() returns null: the slot should render nothing, not an empty box.

How impressions and clicks count

  • An impression counts when Specify serves the ad, so only ask for one when the slot is on the page and about to be shown.
  • Repeats don't add up. The same visitor gets the same ad for a day in each image format, and counts once. See Payments.
  • A click counts when the visitor follows ctaUrl. Link your call to action to it exactly as given, without rewriting it.

Serving from a server component

app/page.tsx
import { serve, ImageFormat } from '@specify-sh/publisher-sdk/server';
import { getSession } from '@/auth';

export default async function Page() {
  const { wallets } = await getSession();
  const ad = await serve({
    publisherKey: process.env.NEXT_PUBLIC_SPECIFY_PUBLISHER_KEY!,
    walletAddresses: wallets,
    imageFormat: ImageFormat.LANDSCAPE,
    adUnitId: 'sidebar'
  });

  if (!ad) return null;

  return <a href={ad.ctaUrl}>{ad.headline}</a>;
}

walletAddresses needs at least one address here, because a server request has no cookie to fall back on.

Importing the wrong entrypoint

Importing @specify-sh/publisher-sdk or @specify-sh/publisher-sdk/react from a server component fails the build, with a message telling you to add 'use client' or use @specify-sh/publisher-sdk/server.

Next steps

On this page