Node.js quickstart with JavaScript
Read the public Advanced Trade Bitcoin/USD product with CBAdvancedTradeClient. This example uses plain JavaScript, with no compiler or API credentials.
Installation
TypeScript definitions are included.
Install the npm package with your preferred tool:
npm install coinbase-apipnpm add coinbase-apiyarn add coinbase-apibun add coinbase-apideno add npm:coinbase-apiPackage: coinbase-api
npm: https://www.npmjs.com/package/coinbase-apiFor other npm-compatible tools, use the same package from npm. Runtime-specific setup is covered below.
Choose your setup
Follow the guide for your runtime or framework. Each includes a complete project with commands to install dependencies and run it.
| Setup | Guides |
|---|---|
| Node.js | JavaScript or TypeScript |
| Alternative runtimes | Bun or Deno |
| Browser apps | React with Vite or Vite with plain JavaScript |
| Next.js | Node.js server Route Handler |
| Cloudflare Workers | Public prices and authenticated orders |
| Mobile apps | React Native / Expo for iOS and Android |
Browser and Expo guides include SDK-specific bundler configuration and polyfills where needed. See Runtime setup for project recipes and required configuration.
Use Node.js 24 for this quickstart. For other runtimes and required configuration, see Runtime setup.
Make a request
Save this as prices.mjs:
import { CBAdvancedTradeClient } from 'coinbase-api';
async function main() {
const client = new CBAdvancedTradeClient();
const product = await client.getPublicProduct({ product_id: 'BTC-USD' });
console.log(`${product.product_id} price: ${product.price}`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});node prices.mjsExample output
BTC-USD price: 65000The price is illustrative. The method returns the product directly, not an Axios response wrapper. Public REST market data can be cached; use WebSockets for streaming prices.
Module formats
The .mjs extension selects ES modules. For CommonJS, save a .cjs file and replace the import with const { CBAdvancedTradeClient } = require('coinbase-api');. Keep the async main() function. TypeScript uses the same imports with the package's included types.
The other guides show TypeScript examples, but TypeScript is not required to use the SDK. If you choose to run those examples as TypeScript, Node.js 24 can execute their erasable syntax directly without type checking. When copying an example that imports ./authenticated-client.js, compile both TypeScript files with TypeScript first, or change that local import to ./authenticated-client.ts for Node.js type stripping.
Choose a client · Market data · Troubleshooting
TypeScript integrations
The SDK works with both JavaScript and TypeScript. TypeScript is optional, but we strongly recommend it for SDK integrations: the included types help catch invalid method names, parameter shapes and response assumptions at build time, before requests reach the exchange. If you choose TypeScript, follow the TypeScript setup and run npx tsc --noEmit during development and CI. Type checking does not replace runtime validation or exchange-side checks.
Some runtimes can execute TypeScript directly. For example, Node.js type stripping supports erasable TypeScript syntax without a separate compiler, but does not check types. If your runtime cannot execute the syntax used by a TypeScript example, compile that example to JavaScript or use a compatible TypeScript runner. Examples with local .js imports that refer to TypeScript source files can use the compiler-based setup above. Plain JavaScript usage does not need any TypeScript tooling.
See Advanced Trade sandbox and Exchange sandbox for setup, credentials and limitations.