Next.js¶
@oura/nextjs · source
Server-side helpers for the Next.js App Router: create payments in server actions or route handlers, and
verify webhooks with a one-line route. Built on @oura/sdk.
Create a payment in a server action¶
'use server';
import { Oura } from '@oura/nextjs';
const oura = new Oura(process.env.OURA_SECRET_KEY!);
export async function startCheckout(orderId: string) {
const payment = await oura.payments.create({
amount: 245000,
methods: ['esewa', 'khalti'],
reference: orderId,
return_url: `https://himalstore.com.np/orders/${orderId}`,
});
return payment.checkout_url; // pass to <OuraCheckoutButton> from @oura/react
}
Webhook route¶
// app/api/webhooks/oura/route.ts
import { ouraWebhookRoute } from '@oura/nextjs';
export const runtime = 'nodejs'; // signature verification needs node:crypto
export const POST = ouraWebhookRoute({
secret: process.env.OURA_WEBHOOK_SECRET!,
onEvent: async (event) => {
if (event.type === 'payment.succeeded') {
// fulfil the order — event.data.payment_id
}
},
});
The route reads the raw body, verifies the signature in constant time with replay protection, and returns
400 for anything that fails. onEvent only runs for trusted events.
Use the Node.js runtime
Webhook verification needs node:crypto. Keep runtime = 'nodejs'; the Edge runtime won't work.