0
0
NextJSframework~20 mins

Route handlers for webhooks in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Webhook Route Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js route handler for a webhook POST request?

Consider this Next.js route handler in the app/api/webhook/route.ts file. What will be the response when a POST request with JSON body {"event":"payment_succeeded"} is sent?

NextJS
import { NextResponse } from 'next/server';

export async function POST(request: Request) {
  const data = await request.json();
  if (data.event === 'payment_succeeded') {
    return NextResponse.json({ status: 'success', message: 'Payment processed' });
  }
  return NextResponse.json({ status: 'ignored' });
}
A{"status":"ignored"}
B{"status":"success","message":"Payment processed"}
CSyntaxError due to missing await
DTypeError because request.json is not a function
Attempts:
2 left
💡 Hint

Check how the JSON body is read and how the condition matches the event.

📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in this Next.js webhook route handler?

Identify which code snippet will cause a syntax error when used as a Next.js route handler for a webhook.

Aexport async function POST(request: Request) { const data = await request.json(); return new Response('OK'); }
Bexport async function POST(request: Request) { const data = await request.json(); return new Response(JSON.stringify({ok:true}), { headers: { 'Content-Type': 'application/json' } }); }
C} ;)'KO'(esnopseR wen nruter ;)(nosj.tseuqer tiawa = atad tsnoc { )tseuqeR :tseuqer(TSOP noitcnuf cnysa tropxe
Dexport async function POST(request: Request) { const data = await request.json(); return Response.json({ok:true}); }
Attempts:
2 left
💡 Hint

Look for missing semicolons or incorrect method calls.

🔧 Debug
advanced
2:00remaining
Why does this Next.js webhook route handler fail to process the ping event?

Given this route handler code, why does it fail to return 'pong' when a POST request with JSON body {"event":"ping"} is made?

NextJS
export async function POST(request: Request) {
  const data = request.json();
  if (data.event === 'ping') {
    return new Response('pong');
  }
  return new Response('ignored');
}
ABecause request.json() returns a Promise and is not awaited before accessing data.event
BBecause the event property does not exist on the request object
CBecause POST function must be synchronous, not async
DBecause new Response requires a JSON string, not plain text
Attempts:
2 left
💡 Hint

Check how asynchronous functions and promises are handled.

state_output
advanced
2:00remaining
What is the value of the response body when this Next.js webhook route handler receives a GET request?

Analyze this route handler code. What will be the response body if a GET request is sent to this webhook route?

NextJS
import { NextResponse } from 'next/server';

export async function POST(request: Request) {
  return NextResponse.json({ message: 'POST received' });
}

export async function GET(request: Request) {
  return NextResponse.json({ message: 'GET not supported' });
}
A{"message":"GET not supported"}
B{"message":"POST received"}
C404 Not Found error
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Check which function handles GET requests.

🧠 Conceptual
expert
3:00remaining
Which option correctly implements a Next.js webhook route handler that verifies a signature header before processing?

You want to create a Next.js route handler for a webhook that first checks the X-Signature header matches a secret before reading the JSON body. Which code snippet correctly implements this?

A
export async function POST(request: Request) {
  const signature = request.headers['X-Signature'];
  if (signature !== process.env.WEBHOOK_SECRET) {
    return new Response('Unauthorized', { status: 401 });
  }
  const data = await request.json();
  return new Response('OK');
}
B
export async function POST(request: Request) {
  const signature = request.headers.get('x-signature');
  if (signature !== process.env.WEBHOOK_SECRET) {
    return new Response('Unauthorized', { status: 403 });
  }
  const data = await request.json();
  return new Response('OK');
}
C
export async function POST(request: Request) {
  const signature = request.headers.get('X-Signature');
  if (signature !== process.env.WEBHOOK_SECRET) {
    return new Response('Unauthorized', { status: 401 });
  }
  const data = await request.json();
  return new Response('OK');
}
D
export async function POST(request: Request) {
  const signature = request.headers.get('X-Signature');
  if (signature === process.env.WEBHOOK_SECRET) {
    return new Response('Unauthorized', { status: 401 });
  }
  const data = await request.json();
  return new Response('OK');
}
Attempts:
2 left
💡 Hint

Remember how to get headers in Next.js route handlers and the correct status code for unauthorized access.