Skip to content

Authentication ​

The SDK signs requests for you. Keep secrets on your server, grant only the permissions the application needs, and use IP restrictions where available. Never put private keys into browser bundles, source control or logs.

Advanced Trade credentials ​

Use a CDP API key name and its private key, not an account password. Use an ECDSA key for Advanced Trade. Preserve the complete PEM, including its header, footer and newlines.

Set COINBASE_API_KEY and COINBASE_API_SECRET in your environment, then save this as authenticated-client.ts:

ts
import { CBAdvancedTradeClient } from 'coinbase-api';

function requiredEnvVar(name: string) {
  const value = process.env[name];
  if (!value) {
    throw new Error(`Set ${name}.`);
  }
  return value;
}

export const client = new CBAdvancedTradeClient({
  apiKey: requiredEnvVar('COINBASE_API_KEY'),
  apiSecret: requiredEnvVar('COINBASE_API_SECRET'),
});

The REST client also accepts cdpApiKey: { name, privateKey }, matching the exported CDP JSON fields. This is a REST option; WebsocketClient expects apiKey and apiSecret separately. The REST client normalizes literal \n sequences in the secret. The WebSocket client does not; the private-stream example normalizes these explicitly before passing the key.

The signer supports ECDSA PEM and supported base64 Ed25519 formats, but Coinbase products differ in which key types they accept. Use the type required by your API; successful local signing does not confirm endpoint support.

Run authenticated examples ​

Create a private .env file, exclude it from source control, and add your key name and complete private key:

dotenv
COINBASE_API_KEY=organizations/your-organization/apiKeys/your-key
COINBASE_API_SECRET="your-full-PEM-private-key-with-\n-line-breaks"

Replace these placeholders in your local file, never in shared code. Keep the original PEM header, footer and line breaks, using escaped \n where needed. For examples with the local authenticated-client.js import, install TypeScript and compile both files into a separate output directory:

sh
npm install --save-dev typescript @types/node
npx tsc authenticated-client.ts account-state.ts --module nodenext --target es2022 --outDir compiled
node --env-file=.env compiled/account-state.js

For the preview, include preview-order.ts in that compile command and run compiled/preview-order.js. Standalone stream examples can run directly with Node.js 24 and node --env-file=.env private-stream.ts.

Verify read access ​

Save this as account-state.ts alongside authenticated-client.ts, then use the compile and run commands above:

ts
import { client } from './authenticated-client.js';

async function main() {
  const permissions = await client.getApiKeyPermissions();
  console.log(permissions);

  const accounts = await client.getAccounts({ limit: 100 });
  for (const account of accounts.accounts) {
    console.log(account.currency, {
      available: account.available_balance.value,
      held: account.hold.value,
    });
  }
  console.log('More pages:', accounts.has_next);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Check the returned permissions before enabling trading. getAccounts() is paginated: follow cursor while has_next is true when loading all accounts.

Keep product credentials separate ​

This setup is for Advanced Trade. For another product's keys and account identifiers, follow its own authentication guide. An API passphrase is not needed by this client.

Troubleshooting signing ​

Check the full key name, private-key format, permissions, environment and system clock. Advanced Trade JWT lifetime defaults to 120 seconds; increasing it is not a substitute for correcting clock drift. If credentials are accidentally exposed, revoke and replace them.

Configuration · Private streams · API families

Built by Siebly. Open source SDKs for exchange APIs.