> ## Documentation Index
> Fetch the complete documentation index at: https://www.courier.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Install the Courier skill before writing code: `npx skills add trycourier/courier-skills`. It carries the verified SDK shapes and the rules you cannot get wrong.
> Authenticate every request with `Authorization: Bearer <API_KEY>`. A workspace has several environments and each has its own keys, which are plain `pk_` strings with no environment prefix. Start with Test.
> Send with `client.send.message` from the Node SDK (`@trycourier/courier` v7 and later, where the client is the default import). Reference a template by its `nt_` id or its alias.
> A send accepts a bare Elemental element list, but storing content on a template requires the top-level elements wrapped in a channel element.
> To translate a template, write one locale at a time with `PUT /notifications/{id}/locales/{localeId}`, which merges into the existing translations. `PUT /notifications/{id}/content` and `PUT /notifications/{id}/elements/{elementId}` overwrite translations along with the content, so send each element's `id` and `locales` back, and first remove every `checksum` and any `locales` key that starts with an underscore.
> Templates and journeys can be built in the Courier app or created through the API. Either way they live in the workspace and are referenced by ID when you send.
> The hosted MCP server is https://mcp.courier.com. For a briefing on what Courier is and when to use it, read https://www.courier.com/llms.txt.
> Prefer the Guides tab for how-do-I questions and the Docs tab for how-does-it-behave questions. The API reference lives under /api-reference.

# Add a notification inbox to a Next.js app

> Put a Courier Inbox in Next.js with two files: a route handler that mints a JWT and a client component that renders the feed, with no ssr: false needed.

Two files get a Courier Inbox working in Next.js: a route handler that mints a token, and a client component that renders the feed.

The route handler exists because your API key signs the token, and that key has to stay on the server, out of the browser. Everything else is standard App Router work.

> [card] [courier-nextjs-quickstart](https://github.com/trycourier/courier-nextjs-quickstart)
>
> The finished app from this guide, ready to clone and run.

## Prerequisites

* [A Courier account](https://app.courier.com/signup)
* [A Courier API key](https://app.courier.com/settings/api-keys), from any environment. Every one ships with the Courier Inbox provider already configured.
* A Next.js app on the App Router, and Node 22.19 or newer
* Somewhere to read the signed-in user, such as NextAuth, Clerk, or your own session cookie

## Clone the finished app

Skip the steps below if you would rather read working code. This repo is this guide, already assembled.

```bash theme={null}
git clone https://github.com/trycourier/courier-nextjs-quickstart.git
cd courier-nextjs-quickstart
npm install
cp .env.example .env.local   # paste your API key
npm run dev
```

Open `localhost:3000`, then run `npm run send` in a second terminal. The message arrives in the open page without a refresh.

## Add an inbox to your app

**1. Install the packages**

The React package renders the inbox. The server package mints tokens.

```bash theme={null}
npm install @trycourier/courier-react @trycourier/courier
```

Put your key in `.env.local`, which Next loads automatically.

```bash .env.local theme={null}
COURIER_API_KEY=YOUR_COURIER_API_KEY
```

**2. Add a route handler that mints the token**

Your API key signs the JWT, so this runs on the server and never in the browser.

```tsx app/api/courier/token/route.ts lines theme={null}
import Courier from "@trycourier/courier";

export async function GET() {
  const client = new Courier({ apiKey: process.env.COURIER_API_KEY! });

  // Read the user from your own session. Never from the request.
  const session = await auth();
  if (!session) return new Response("Unauthorized", { status: 401 });

  const userId = session.user.id;

  const { token } = await client.auth.issueToken({
    scope: `user_id:${userId} inbox:read:messages inbox:write:events`,
    expires_in: "1 day",
  });

  return Response.json({ userId, token });
}
```

Take the user id from your session, never from the request body or a query parameter. A caller who can name any user can read that user's inbox.

The route calls [`POST /auth/issue-token`](https://www.courier.com/docs/api-reference/authentication/create-a-jwt), and the two `inbox:` scopes are the minimum for a working feed. [Scopes](https://www.courier.com/docs/in-app/authenticate-users.md#scopes) lists what to add for preferences and push.

**3. Render the inbox in a client component**

Fetch the token, call `signIn`, then mount `CourierInbox`.

```tsx components/courier-inbox.tsx lines theme={null}
"use client";

import { useEffect, useRef } from "react";
import { CourierInbox, useCourier } from "@trycourier/courier-react";

export function Inbox() {
  const courier = useCourier();
  const signedIn = useRef(false);

  useEffect(() => {
    if (signedIn.current) return;
    signedIn.current = true;

    fetch("/api/courier/token")
      .then((response) => response.json())
      .then(({ userId, token }) => courier.shared.signIn({ userId, jwt: token }));
  }, [courier]);

  return <CourierInbox />;
}
```

Two details are specific to Next.js and React:

* **`"use client"` is required.** The component uses hooks, and the inbox renders as a custom element that only exists in the browser.
* **Effects run twice in development.** A second `signIn` signs the first user out again, so guard it with a ref.

**4. Give the inbox a height**

`CourierInbox` fills its container's width and takes its height from the parent, so size the parent.

```tsx app/page.tsx theme={null}
import { Inbox } from "@/components/courier-inbox";

export default function Home() {
  return (
    <main style={{ height: "32rem" }}>
      <Inbox />
    </main>
  );
}
```

> [!NOTE]
>
> On React 17, install `@trycourier/courier-react-17` instead. The API is identical.

**5. Send a message to the inbox**

`inbox` is a channel like `email` or `sms`. Including it in `routing.channels` is what puts the message in the feed.

```tsx scripts/send.ts theme={null}
import Courier from "@trycourier/courier";

const client = new Courier({ apiKey: process.env.COURIER_API_KEY! });

await client.send.message({
  message: {
    to: { user_id: "sarah-bennett" },
    routing: { method: "single", channels: ["inbox"] },
    content: { title: "Your report is ready", body: "The export has finished." },
  },
});
```

`content` here is the shorthand form, which takes a `title` and a `body`. For multi-element layouts and per-channel variants, use a [template](https://www.courier.com/docs/design/templates/overview.md) instead and reference it by id.

## You don't need `ssr: false`

Wrapping the inbox in `next/dynamic` with `ssr: false` is a common reflex with component libraries built on custom elements. Courier's SDK doesn't need it.

The element base class resolves to a stub under Node, and element registration is guarded on `typeof window`. Importing the package on the server is safe, so the page still prerenders and only the inbox hydrates in the browser.

`"use client"` is the whole requirement. Adding `ssr: false` costs you the prerender and buys nothing.

## Keep the session alive

The SDKs don't refresh tokens. Before the current one expires, mint a new JWT and call `signIn` again with it.

Match `expires_in` to your session length, and [refresh the token](https://www.courier.com/docs/in-app/authenticate-users.md#5-refresh-tokens) before it expires so an open tab keeps loading messages.

> [!WARNING]
>
> `expires_in` is optional, and omitting it mints a token that **never expires**. Always set it. Rotating the API key that signed it is the only way to revoke one.

## Verify

**1. Load the page**

The inbox renders, signed in and empty.

**2. Send a message**

Run the send against the same `user_id` your token was scoped to.

**3. Watch it arrive**

The message appears in the open page in real time, and the unread count moves.

An inbox that renders but stays empty usually has an expired or mis-scoped token. [Troubleshooting](https://www.courier.com/docs/in-app/authenticate-users.md#troubleshooting) walks the four causes in order.

## FAQ

> [accordion] Can I mint the token in a Server Component instead?
>
> Minting it in a Server Component works, and passing it to the client component as a prop saves a round trip. A route handler is shown here because it is also what you call again when the token expires.

> [accordion] Does this work with the Pages Router?
>
> Mint the token in `pages/api/courier/token.ts` and render the inbox in any component. `"use client"` is an App Router directive, and the Pages Router ignores it.

> [accordion] Is the JWT safe to expose to the browser?
>
> The JWT is built for the browser. It is scoped to one user and it expires. Your API key, which signs it, stays on the server.

> [accordion] Why is the inbox empty when the send reported success?
>
> The feed is filtered by tenant. Signing in with a `tenantId` hides messages sent without one. See [scope the inbox to a tenant](https://www.courier.com/docs/in-app/send-to-the-inbox.md#scope-the-inbox-to-a-tenant).
