Next.js server quickstart
Serve a public BTC price from a Next.js App Router Route Handler using okx-api. No API key is needed. Prices are BTC-USDT last traded prices. This example runs in the Node.js server runtime.
Use Node.js 24 and npm. You can also view the complete OKX Next.js example on GitHub.
See Runtime compatibility for runtime requirements.
These examples use public market data. Keep authenticated account and trading calls on your server. See Runtime setup for the scope of each recipe; a successful build does not verify live exchange access.
Create the project
mkdir okx-next-quickstart
cd okx-next-quickstart1. Create package.json
Declare the SDK dependency and the commands used by this project.
{
"name": "okx-next-quickstart",
"private": true,
"type": "module",
"scripts": {
"dev": "next dev --webpack",
"build": "next build --webpack",
"start": "next start"
},
"dependencies": {
"okx-api": "latest",
"next": "16.4.5",
"react": "19.2.0",
"react-dom": "19.2.0"
}
}2. Install dependencies
Run this after saving package.json:
npm install3. Create app/layout.js
Provide the root HTML layout required by the Next.js App Router.
Create its directory first:
mkdir -p appexport 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:
mkdir -p app/api/priceimport { RestClient } from 'okx-api';
export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';
export async function GET() {
try {
const client = new RestClient();
const tickers = await client.getTicker({ instId: 'BTC-USDT' });
const price = tickers[0]?.last;
if (!price) {
throw new Error('No price was returned.');
}
return Response.json({
symbol: 'BTC-USDT',
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.
export default function Page() {
return (
<main>
<h1>Current price</h1>
<a href="/api/price">Read the current price</a>
</main>
);
}Run
npm run devOpen http://localhost:3000/api/price, or request it in a second terminal:
curl --fail http://localhost:3000/api/priceA successful response contains a positive price string, for example:
{
"symbol": "BTC-USDT",
"price": "65000.5"
}Check the production server with:
npm run build
npm startThe 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
502means 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 installfrom the directory containing this starter'spackage.json.