Add your key
Put the key in.env.local, next to package.json:
.env.local
NEXT_PUBLIC_ prefix: Next.js would put it in the code it sends to the browser.
Call Leap from the server
Both routes call Leap through this helper. It sends your key, and it answers the browser with only what the page shows: the ID, the status, the output links and any error.lib/leap.ts
Start a run
POST /api/generate takes a prompt and whether to make an image or a video. The browser picks the kind, never the model, so it can only run what you chose.
app/api/generate/route.ts
prefer: wait=50 holds the request open until the image is ready, for up to 50 seconds, so most images come back done and the page doesn’t poll at all. The wait has to fit inside your function’s time limit. maxDuration asks the platform for 60 seconds; if your plan allows less, send a shorter wait, or none, and let the page poll images too. An image that isn’t done when the wait ends comes back queued or running, and the page polls it like a video.
@/ is the import alias that create-next-app sets up. If your app has none, use a relative path.
Read a run
GET /api/generations/{id} reads the run’s status. In Next.js 15 and later, a route handler’s params is a promise, so await it.
app/api/generations/[id]/route.ts
Show it on a page
The client component starts a run, then polls every 5 seconds until the status issucceeded, failed or canceled, and shows the image or video.
app/generate.tsx
app/page.tsx
npm run dev, open the page, and make an image. With the defaults above, an image costs $0.033 and a 4-second video with sound $0.66.
Before you ship
- Check that each generation belongs to the person asking. Anyone who can call these routes spends your balance, and anyone who knows a generation’s ID can read its output. Sign people in, save each ID with the user who started it when you start the run, and in the
GETroute answer404unless the ID is theirs. - Output links expire after 24 hours. To keep a result, download it on the server and store it yourself.
- Every request reaches Leap from your server, so they share its IP address’s budget of 300 requests a minute and your key’s 600. Each open page polls once every 5 seconds while a run is going. See Rate limits.
- A failed run isn’t charged, and
error.messagesays why. The page shows it as it is. See When a run fails.