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

# Next.js

> Add Nelkap to a Next.js app with the App Router, from your keys to your first run.

This guide adds Nelkap to a Next.js app that uses the App Router. By the end, your app will run a workflow that creates a new user's profile, waits a day, and sends them a welcome email, and you'll follow each step in your project.

You need Next.js 15.1 or later, and a Nelkap project with its endpoint, an API key, and its secret. If you don't have them yet, follow the first three steps of the [quickstart](https://docs.nelkap.com/quickstart.md).

**1. Install the SDK**

```bash npm theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
npm install @nelkap/sdk
```

```bash pnpm theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
pnpm add @nelkap/sdk
```

```bash yarn theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
yarn add @nelkap/sdk
```

**2. Add your keys**

Add your project's API key and secret to `.env.local`:

```bash .env.local theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
NELKAP_API_KEY=your-api-key
NELKAP_SECRET=your-project-secret
```

Next.js loads this file into `process.env`, where the SDK reads both values. The API key lets your app start runs and save steps. The secret lets your app check that each call really comes from Nelkap.

When you deploy, set the same two variables in your host's settings, like your Vercel project's environment variables.

**3. Create your client and your first workflow**

Create `lib/nelkap.ts`. It holds the Nelkap client, your workflow, and the list of workflows your app runs:

```ts lib/nelkap.ts theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import { Nelkap } from '@nelkap/sdk';

import { createProfile, sendWelcomeEmail } from '@/lib/activities';

export const nelkap = new Nelkap();

export async function onboardUser(userId: string) {
  await nelkap.step.run('profile', () => createProfile(userId));

  await nelkap.step.sleep('1 day');

  await nelkap.step.run('welcome', () => sendWelcomeEmail(userId));
}

nelkap.registerWorkflows([{ name: 'onboardUser', handler: onboardUser }]);
```

* `onboardUser` is your workflow: an async function that takes the run's payload, here a user ID.
* Each `step.run` saves what its function returns. When the run picks up after the sleep, the profile step gives back its saved result instead of creating the profile again.
* `step.sleep` pauses the run for a day. Nothing runs in your app during that day.
* `registerWorkflows` tells Nelkap which workflows your app runs, as soon as the file is loaded.

Replace `createProfile` and `sendWelcomeEmail` with your own functions.

**4. Add the route Nelkap calls**

Create a route handler at the path of your endpoint, and pass each request to `nelkap.handle`:

```ts app/api/nelkap/route.ts theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import { after } from 'next/server';

import { nelkap } from '@/lib/nelkap';

export async function POST(request: Request) {
  return nelkap.handle(request, { waitUntil: (promise) => after(() => promise) });
}
```

`handle` checks the secret, answers Nelkap at once, and runs the workflow in the background. [`after`](https://nextjs.org/docs/app/api-reference/functions/after) keeps that background work going once the answer is sent, on Vercel as on your own server.

If your app checks sign-in on every route, for example in a proxy or a middleware, leave `/api/nelkap` out. Nelkap doesn't sign in: `handle` checks its secret instead.

**5. Deploy and set your endpoint**

Deploy your app, so Nelkap can reach the route over the internet. Then edit your project and set its endpoint to the route's URL, like `https://your-app.com/api/nelkap`.

**6. Start a run**

Start the workflow from any server code: a Server Action, a route handler, or a webhook. Here, a Server Action starts it when someone signs up:

```ts app/actions.ts theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
'use server';

import { nelkap, onboardUser } from '@/lib/nelkap';
import { createUser } from '@/lib/users';

export async function signUp(formData: FormData) {
  const user = await createUser(formData);

  await nelkap.run(onboardUser, user.id);
}
```

`run` returns as soon as Nelkap has the run, so the sign-up doesn't wait for the workflow. Its second argument, `user.id`, is the run's payload: Nelkap passes it to `onboardUser`.

**7. Follow your run**

Open your project's runs. The new run shows the profile step with what it returned, then sleeps. A day later, Nelkap calls your route again, the welcome email goes out, and the run completes.

## Long runs on Vercel

On Vercel, `after` keeps a call going for as long as your function may run. If a call reaches that limit, every step already saved is kept, and Nelkap calls your app again about five minutes later to go on from there.

If a single step needs more time than the default limit, raise it on the route with [`maxDuration`](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config/maxDuration), in seconds, within what your Vercel plan allows:

```ts app/api/nelkap/route.ts theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
export const maxDuration = 300;
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.