Home » Guide: Fix API resolved without sending a response for /api/ in Next.js
Web & Code

Guide: Fix API resolved without sending a response for /api/ in Next.js

✨ Quick Answer

The warning API resolved without sending a response for /api/... in Next.js occurs when an API Route Handler or Pages API endpoint executes asynchronous logic (such as database queries, third-party APIs, or webhooks) without await or without resolving a returned Promise before the handler function finishes execution.

The warning API resolved without sending a response for /api/... in Next.js occurs when an API Route Handler or Pages API endpoint executes asynchronous logic (such as database queries, third-party APIs, or webhooks) without await or without resolving a returned Promise before the handler function finishes execution.

This issue can cause Node.js server connections to hang, resulting in frontend request timeouts, memory leaks, and dropped HTTP responses.

Quick Diagnostics

Cause
Unawaited async Promise (missing await)
Solution
Prefix all database queries and external fetch requests with await
Cause
Callback-based middleware (Stripe webhooks, JWT)
Solution
Wrap legacy callback functions in new Promise((resolve, reject) => ...)
Cause
Missing response in conditional branch
Solution
Ensure every if/else execution path returns a valid res.status() or Response

Step-by-Step Solution

Ensure proper async/await flow in your handler If you are using the Pages Router (pages/api/...), every asynchronous database call must be completed before returning the response:

JAVASCRIPT
// pages/api/users.js
export default async function handler(req, res) {
  try {
    // Omitting await will trigger the unhandled resolution warning
    const users = await db.collection('users').find().toArray();
    return res.status(200).json({ success: true, data: users });
  } catch (error) {
    return res.status(500).json({ success: false, error: error.message });
  }
}

Wrap callback-based libraries inside Promises When dealing with older packages that do not provide native Promises (e.g. jsonwebtoken or multer), explicitly wrap the call:

JAVASCRIPT
export default async function handler(req, res) {
  await new Promise((resolve) => {
    jwt.verify(token, secretKey, (err, decoded) => {
      if (err) {
        res.status(401).json({ error: 'Invalid token' });
        return resolve();
      }
      res.status(200).json({ user: decoded });
      return resolve();
    });
  });
}

Use App Router Route Handlers (Next.js 13+) For the modern App Router (app/api/.../route.js), return native web standard Response or NextResponse instances:

JAVASCRIPT
// app/api/users/route.js
import { NextResponse } from 'next/server';

export async function GET(request) {
  const data = await fetchUserData();
  return NextResponse.json(data);
}

Prevention Advice

Recommended security practices:

  • ESLint floating promise rule: Enable @typescript-eslint/no-floating-promises to catch unhandled async promises at build time.
  • Client timeout limits: Configure timeout handlers on database drivers to guarantee requests fail gracefully instead of hanging connections indefinitely.