/** * Derive L2 API keys from your Polymarket wallet. * * Usage: * pnpm derive-keys * * Prerequisites: * - Set PRIVATE_KEY in .env (your Polymarket signer key) * - Set FUNDER_ADDRESS in .env (your Polymarket proxy wallet) * - Set SIGNATURE_TYPE in .env (1 for POLY_PROXY) * * This script will: * 1. Create a ClobClient with your wallet * 2. Call /auth/derive-api-key to get L2 API credentials * 3. Print the credentials so you can add them to .env */ import { ClobClient } from '@polymarket/clob-client'; import { Wallet } from '@ethersproject/wallet'; const CLOB_HOST = 'https://clob.polymarket.com'; const CHAIN_ID = 137; async function main() { const privateKey = process.env['PRIVATE_KEY']; const funderAddress = process.env['FUNDER_ADDRESS']; const signatureType = parseInt(process.env['SIGNATURE_TYPE'] || '1', 10); if (!privateKey) { console.error('❌ PRIVATE_KEY not set in .env'); console.error(' Export your key from Polymarket: Settings > Export Private Key'); process.exit(1); } if (!funderAddress) { console.error('❌ FUNDER_ADDRESS not set in .env'); console.error(' This is your Polymarket deposit/proxy wallet address'); process.exit(1); } const wallet = new Wallet(privateKey); console.log('🔑 Deriving L2 API keys from your Polymarket wallet...\n'); console.log(` Signer: ${wallet.address}`); console.log(` Funder: ${funderAddress}`); console.log(` Signature Type: ${signatureType} (${signatureType === 1 ? 'POLY_PROXY' : 'EOA'})`); console.log(); try { const client = new ClobClient( CLOB_HOST, CHAIN_ID, wallet, undefined, // creds — will be derived signatureType, funderAddress, ); const creds = await client.createOrDeriveApiKey(); console.log('✅ L2 API keys derived successfully!\n'); console.log('Add these to your .env file:\n'); console.log(`L2_API_KEY=${creds.key}`); console.log(`L2_API_SECRET=${creds.secret}`); console.log(`L2_API_PASSPHRASE=${creds.passphrase}`); console.log(); console.log('⚠️ Keep these secret! Do not commit .env to git.'); } catch (err) { console.error('❌ Failed to derive API keys:', err); process.exit(1); } } main();