How to create, derive, and manage API keys for authenticated CLOB operations
API keys are required for authenticated operations like posting orders, viewing trades, and managing your account. This guide covers the complete lifecycle of API key management.
import { ClobClient, Chain } from "@polymarket/clob-client";import { ethers } from "ethers";const wallet = new ethers.Wallet("your-private-key");const host = "https://clob.polymarket.com";const chainId = Chain.POLYGON; // 137const clobClient = new ClobClient(host, chainId, wallet);
2
Create or Derive API Key
Use createOrDeriveApiKey() to get credentials:
// This will create a new key if none exists, or derive the existing oneconst apiKeyCreds = await clobClient.createOrDeriveApiKey();console.log("API Key:", apiKeyCreds.key);console.log("Secret:", apiKeyCreds.secret);console.log("Passphrase:", apiKeyCreds.passphrase);
Store these credentials securely! The secret and passphrase cannot be retrieved later.
3
Initialize Client with Credentials
Create a new client instance with your API credentials:
import { ApiKeyCreds } from "@polymarket/clob-client";const creds: ApiKeyCreds = { key: "your-api-key", secret: "your-secret", passphrase: "your-passphrase",};const clobClient = new ClobClient( host, chainId, wallet, creds // Add credentials for L2 auth);// Now you can use authenticated endpointsconst openOrders = await clobClient.getOpenOrders();
// Create a readonly API keyconst readonlyKey = await clobClient.createReadonlyApiKey();console.log("Readonly API key:", readonlyKey.apiKey);// List all readonly keysconst readonlyKeys = await clobClient.getReadonlyApiKeys();console.log("All readonly keys:", readonlyKeys);
// Initialize client with readonly key (no secret or passphrase needed)const readonlyClient = new ClobClient(host, chainId);// Query open orders for any addressconst openOrders = await readonlyClient.getOpenOrders({ maker_address: "0x123...",});