Skip to content
Docs
Usage guide
App Router API Route Handlers

App Router API Route Handlers

Here’s an example of how to use the getTokens function in API Route Handlers (opens in a new tab).

import { NextRequest, NextResponse } from "next/server";
import { getTokens } from "next-firebase-auth-edge";
 
export async function GET(request: NextRequest) {
  const tokens = await getTokens(request.cookies, {
    apiKey: 'XXxxXxXXXxXxxxxx_XxxxXxxxxxXxxxXXXxxXxX',
    cookieName: 'AuthToken',
    cookieSignatureKeys: ['Key-Should-Be-at-least-32-bytes-in-length'],
    serviceAccount: {
      projectId: 'your-firebase-project-id',
      clientEmail: 'firebase-adminsdk-nnw48@your-firebase-project-id.iam.gserviceaccount.com',
      privateKey: '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n'
    }
  });
 
  if (!tokens) {
    throw new Error("Unauthenticated");
  }
 
  const headers: Record<string, string> = {
    "Content-Type": "application/json",
  };
 
  const response = new NextResponse(
    JSON.stringify({
      tokens,
    }),
    {
      status: 200,
      headers,
    }
  );
 
  return response;
}