Skip to content

Next.js server quickstart ​

Serve a public BTC price from a Next.js App Router Route Handler using coinbase-api. No API key is needed. Prices are BTC-USD last traded prices. This example runs in the Node.js server runtime.

Use Node.js 24 and npm. You can also view the complete Coinbase Next.js example on GitHub.

See Runtime compatibility for runtime requirements.

This project uses Advanced Trade public market data. Keep credentialed account and trading calls in a server-side application.

Create the project ​

sh
mkdir coinbase-next-quickstart
cd coinbase-next-quickstart

1. Create package.json ​

Declare the SDK dependency and the commands used by this project.

json
{
  "name": "coinbase-next-quickstart",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "next dev --webpack",
    "build": "next build --webpack",
    "start": "next start"
  },
  "dependencies": {
    "coinbase-api": "latest",
    "next": "16.4.5",
    "react": "19.2.0",
    "react-dom": "19.2.0"
  }
}

2. Install dependencies ​

Run this after saving package.json:

sh
npm install

3. Create app/layout.js ​

Provide the root HTML layout required by the Next.js App Router.

Create its directory first:

sh
mkdir -p app
js
export default function Layout({ children }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

4. Create app/api/price/route.js ​

Handle the request on the Node.js server. This file creates the SDK client and requests the current price directly.

Create its directory first:

sh
mkdir -p app/api/price
js
import { CBAdvancedTradeClient } from 'coinbase-api';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function GET() {
  try {
    const client = new CBAdvancedTradeClient();
    const ticker = await client.getPublicProduct({ product_id: 'BTC-USD' });
    const price = ticker.price;

    if (!price) {
      throw new Error('No price was returned.');
    }

    return Response.json({
      symbol: 'BTC-USD',
      price: String(price),
    });
  } catch (error) {
    console.error('Price request failed:', error);

    return Response.json(
      { error: 'Exchange price request failed. Check the server logs.' },
      { status: 502 },
    );
  }
}

5. Create app/page.js ​

Add a page with a link to the price endpoint.

js
export default function Page() {
  return (
    <main>
      <h1>Current price</h1>
      <a href="/api/price">Read the current price</a>
    </main>
  );
}

Run ​

sh
npm run dev

Open http://localhost:3000/api/price, or request it in a second terminal:

sh
curl --fail http://localhost:3000/api/price

A successful response contains a positive price string, for example:

json
{
  "symbol": "BTC-USD",
  "price": "65000.5"
}

Check the production server with:

sh
npm run build
npm start

The handler makes a fresh REST request for each call. It does not open a WebSocket. To receive a WebSocket update in a standalone process, use the Node.js, Bun or Deno example. Each closes its subscription explicitly. The Vite example shows browser subscription cleanup.

Troubleshooting ​

  • Keep runtime = 'nodejs' in the handler. Run SDK calls in the server Route Handler, not a Client Component or the Edge runtime.
  • A 502 means the handler could not return a price. Inspect the server log for the error and check outbound HTTPS access from the server, including regional restrictions.
  • If Next.js reports a missing React package, run npm install from the directory containing this starter's package.json.

Built by Siebly. Open source SDKs for exchange APIs.