Crypto API Integration Engineering

Crypto & Web3 Advanced crypto-infrastructure-skills universal
0 Upvotes
3 Views
0 Downloads
2,067 Words

Description

Build fault-tolerant crypto API integrations (CEX/DEX/price oracles) with adapters, normalization, and monitoring.

When to Use

I need to integrate a crypto exchange API reliably | Set up adapters for multiple CEX/DEX APIs | Handle rate limits and retries for crypto APIs | Normalize data from crypto price oracles | Monitor latency and errors across crypto endpoints

Use Cases

Connect to multiple CEXs via a unified adapter layer. | Normalize varying exchange data to a single schema. | Implement retry, backoff, and fallback for crypto APIs. | Monitor latency and error rates across endpoints. | Add price oracle or DEX support with minimal config.

SKILL.md Content

---
name: api-integration-crypto
description: "Build fault-tolerant crypto API integrations (CEX/DEX/price oracles) with adapters, normalization, and monitoring."
metadata:
  tags: "crypto, web3, api-integration, exchange-apis, dex-protocols, price-oracles, defensive-programming, data-normalization"
  source: "https://skilldb.dev/skills/crypto-infrastructure-skills/api-integration-crypto"
  pack: "crypto-infrastructure-skills"
  category: "Crypto & Web3"
---

# Crypto API Integration Engineering

## When to use this skill
Use when the user says things like:
- "I need to integrate a crypto exchange API reliably"
- "Set up adapters for multiple CEX/DEX APIs"
- "Handle rate limits and retries for crypto APIs"
- "Normalize data from crypto price oracles"
- "Monitor latency and errors across crypto endpoints"


You are a world-class integration engineer specializing in cryptocurrency exchange and service APIs. You have built production trading systems that simultaneously connect to dozens of CEXs and DEXs, handling the inconsistencies, rate limits, outages, and breaking changes that are endemic to the crypto API ecosystem. You understand that reliability comes from defensive programming, redundancy, and treating every external API as an unreliable dependency.

## Philosophy

Every external API is a liability. It will go down, it will rate-limit you, it will change without notice, and it will return unexpected data. Your integration layer must be a fortress: validate every response, retry intelligently, fail gracefully, and always have a fallback. Normalize data at the boundary — never let exchange-specific formats leak into your core systems. Build adapters that isolate exchange-specific logic, so swapping or adding an exchange is a configuration change, not a rewrite. Monitor every integration point: latency, error rates, and data quality. An unmonitored integration is a ticking time bomb.

## Core Techniques

### CEX API Integration

**Authentication Patterns**
- HMAC-SHA256 is the dominant pattern (Binance, Bybit, OKX, Kraken). Sign the request parameters with your API secret.
- Binance: sign the query string with HMAC-SHA256. Include `timestamp` and `signature` parameters. Recv window for clock tolerance.
- Coinbase Advanced Trade: sign with HMAC-SHA256 over `timestamp + method + requestPath + body`. Include in headers: `CB-ACCESS-KEY`, `CB-ACCESS-SIGN`, `CB-ACCESS-TIMESTAMP`.
- Kraken: sign with HMAC-SHA512 over nonce + POST data, using SHA256 hash of nonce + data as the key derivation input.
- OKX: sign with HMAC-SHA256 over `timestamp + method + requestPath + body` (Base64 encoded). Pass-phrase required in addition to key/secret.

**Clock Synchronization**
- All exchanges reject requests with stale timestamps. Synchronize your server clock via NTP.
- Measure clock offset: call the exchange's time endpoint, compare to local time, and adjust signatures accordingly.
- Cache the offset and re-measure periodically (every 30 seconds). Network latency introduces ~50-200ms of uncertainty.
- Binance: `recvWindow` parameter (default 5000ms). Set to 10000ms for higher tolerance but do not exceed 60000ms.

**Rate Limiting**

Each exchange has unique rate limit structures:

- Binance: weight-based system. Each endpoint has a weight; total weight per minute is capped (typically 1200/min for orders). Separate limits for orders (10/sec, 100000/day) and data requests.
- Coinbase: varies by endpoint. Typically 10 requests/sec for private, 30/sec for public.
- Kraken: tiered by verification level. Counter-based system with decay. Matching engine limits separate from API limits.
- Bybit: per-endpoint limits (e.g., 10 requests/sec for order placement). IP and UID-based limits.
- OKX: method-specific rate limits per instrument type. 20/2s for order placement.

**Implementation:**
- Track rate limit counters locally. Do not rely solely on response headers (they can be delayed).
- Implement token bucket or sliding window rate limiters per exchange per endpoint category.
- Queue requests when approaching limits. Prioritize order-related requests over data requests.
- Back off exponentially on 429 responses. Include jitter to avoid thundering herd.

**WebSocket Integration**
- All major CEXs offer WebSocket APIs for real-time data. Use them instead of polling REST endpoints.
- Binance: combined streams via `wss://stream.binance.com:9443/stream?streams=`. Subscribe to multiple streams on one connection.
- WebSocket lifecycle: connect, authenticate (for private channels), subscribe, process messages, handle disconnects.
- Implement heartbeat monitoring: send ping frames, track pong responses. Kill and reconnect if pong is not received within timeout.
- Buffer messages during reconnection. Some exchanges support resume-from-sequence (Coinbase, OKX).
- Handle message ordering: WebSocket messages can arrive out of order, especially during high-volume periods.

### DEX Integration

**Uniswap SDK Integration**
- Use the official Uniswap SDKs (`@uniswap/v3-sdk`, `@uniswap/smart-order-router`) for price quoting and route optimization.
- For direct integration: call the Quoter contract (`QuoterV2`) for exact output/input quotes. This simulates the swap on-chain.
- Router contracts: `SwapRouter02` (Uniswap) handles multi-hop swaps. Set appropriate `amountOutMinimum` for slippage protection.
- Multicall: batch multiple quote requests into a single RPC call using the Multicall3 contract.
- Monitor pool liquidity and tick spacing. Thin pools will have high slippage. Check `slot0()` for current tick and `liquidity()` for available liquidity.

**1inch API**
- Aggregator that routes swaps across multiple DEX protocols for best pricing.
- Swap API: `GET /v5.2/{chainId}/swap` — returns transaction data ready to submit.
- Quote API: `GET /v5.2/{chainId}/quote` — price quote without transaction data. Use for price display.
- Set `slippage` parameter (in percentage, e.g., 1 for 1%). Set `disableEstimate` to true for faster responses when you just need a quote.
- Handle partial fill responses: 1inch may not be able to fill the full amount at the quoted price.
- Rate limits: typically 1-3 requests/second for free tier. Use API key for higher limits.

**0x API**
- Professional-grade swap API with RFQ (Request for Quote) support from professional market makers.
- `/swap/v1/quote` for executable quotes, `/swap/v1/price` for indicative pricing.
- RFQ integration provides better pricing for large orders by getting quotes directly from market makers.
- Supports gasless trading via 0x API with permit-based approvals (ERC-2612).

**DEX Integration Best Practices**
- Always simulate swaps before executing. Use `eth_call` to verify the transaction will succeed.
- Set deadlines on all swap transactions. A swap without a deadline can be held and executed later at a worse price (sandwich attack).
- Approve token allowances carefully. Use exact amounts rather than unlimited approvals where possible.
- Monitor for MEV: use Flashbots Protect or private transaction pools to submit swaps if value is significant.

### Price Oracle APIs

**CoinGecko**
- Free tier: 30 calls/minute. Pro tier: 500 calls/minute.
- `/simple/price` for current prices. `/coins/{id}/market_chart` for historical OHLCV.
- Use `vs_currencies` parameter to get prices in multiple quote currencies in one call.
- Data freshness: prices update every 1-2 minutes for popular assets. Not suitable for real-time trading.
- Good for: portfolio valuation, NAV calculation, historical analysis, user-facing price displays.

**CoinMarketCap**
- Professional API with extensive coverage. `Basic` plan: 333 credits/day.
- `/v1/cryptocurrency/quotes/latest` for current prices. Include `CMC_PRO_API_KEY` header.
- Better coverage of smaller-cap tokens than CoinGecko.
- Use for: reference pricing, market cap calculations, token metadata.

**Kaiko**
- Institutional-grade market data. Tick-level data from all major exchanges.
- REST and WebSocket APIs. Aggregated VWAP, OHLCV, orderbook snapshots.
- Suitable for: trading systems needing reliable, normalized multi-exchange data. NAV calculation for funds.
- Pricing: enterprise-level. Consider for institutional operations where data quality justifies cost.

### Blockchain APIs

**Alchemy**
- Enhanced Ethereum API with additional methods: `alchemy_getTokenBalances`, `alchemy_getAssetTransfers`.
- Webhook notifications for address activity, mined/dropped transactions, token transfers.
- NFT API for metadata, ownership, and transfer history.
- Rate limits vary by plan: Free (300 CU/s), Growth (660 CU/s). Compute Units vary by method.
- Supported chains: Ethereum, Polygon, Arbitrum, Optimism, Base, Solana, and more.

**Moralis**
- Web3 data API: wallet balances, token prices, NFT data, transaction history.
- Streams API: real-time webhooks for on-chain events. Define filters for contract events, transfers, etc.
- Good for: application backends that need aggregated blockchain data without running nodes.
- Rate limits: 25 requests/second on Pro plan.

**Covalent (GoldRush)**
- Unified API across 200+ chains. Single endpoint for balances, transactions, and token metadata.
- `GET /v1/{chainId}/address/{address}/balances_v2/` for token balances.
- Historical portfolio value over time. Useful for portfolio tracking and tax reporting.
- Good for: multi-chain applications, analytics dashboards, portfolio trackers.

### Error Handling and Resilience

**Retry Logic**
- Retry on transient errors: HTTP 429 (rate limit), 500 (server error), 502/503 (gateway errors), network timeouts.
- Do not retry on: 400 (bad request — your fault), 401/403 (auth failure — fix keys first), 404 (resource not found).
- Exponential backoff with jitter: `delay = min(base * 2^attempt + random_jitter, max_delay)`.
- Base: 100ms. Max delay: 30 seconds. Max retries: 5 for data queries, 3 for order operations (be cautious with order retries — you might double-submit).
- For order placement retries: check order status before retrying to avoid duplicate orders. Use client order IDs.

**Circuit Breakers**
- Monitor error rates per exchange per endpoint. If error rate exceeds threshold (e.g., 50% over 30 seconds), open the circuit.
- Open circuit: all requests immediately return an error without calling the exchange. Prevents cascading failures.
- Half-open: after a cooldown period (e.g., 30 seconds), allow a single probe request. If it succeeds, close the circuit.
- Separate circuits per exchange and per endpoint category (orders vs market data vs account).

**Timeout Configuration**
- REST API calls: 5-10 seconds for most operations. 30 seconds for historical data queries.
- WebSocket connection: 10-second timeout. Individual message timeout: not applicable (continuous stream).
- Order placement: 3-5 seconds. If you have not received a response, check order status before retrying.
- Blockchain RPC calls: 10-30 seconds depending on method. `eth_call` can be slow on archive queries.

**Idempotency**
- Use client-generated order IDs (`newClientOrderId` on Binance, `clOrdId` on OKX) for all order operations.
- If a timeout occurs during order placement, query by client order ID before retrying.
- Design all operations to be safely retryable. Store the client order ID and check for duplicates before submitting.

### API Key Security

**Key Management**
- Never hardcode API keys in source code. Use environment variables or a secrets manager.
- Secrets managers: AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager, 1Password CLI.
- Rotate keys quarterly or immediately if compromise is suspected.
- Use separate API keys for: production trading, testing, data collection, monitoring. Minimize blast radius if one is compromised.

**Key Permissions**
- Most exchanges allow permission scoping per API key. Use the minimum permissions needed.
- Data-only keys: read access for market data and account info. No trading or withdrawal permissions.
- Trading keys: enable trading, disable withdrawals. Restrict to IP whitelist.
- Withdrawal keys: use only for automated withdrawal systems. Strict IP whitelist. Consider hardware-backed authentication.
- IP whitelist all production API keys. This is the single most effective security measure for exchange API keys.

**Key Rotation**
- Automate key rotation where possible. Generate new key, update configuration, verify connectivity, deactivate old key.
- Keep the old key active for a short overlap period to handle in-flight requests.
- Store key metadata: creation date, last rotation date, permissions, associated service, responsible team member.

## Advanced Patterns

### Unified Exchange Adapter Layer
- Build an adapter interface: `placeOrder(symbol, side, type, quantity, price)`, `cancelOrder(orderId)`, `getBalance(asset)`.
- Each exchange has its own adapter implementation that handles authentication, serialization, and error mapping.
- The trading engine interacts only with the adapter interface. Adding a new exchange means writing a new adapter, not changing core logic.
- Normalize error codes: map exchange-specific errors to a common error taxonomy (RateLimited, InsufficientBalance, InvalidOrder, ExchangeDown).

### Smart Order Routing
- Route orders to the exchange with the best price, considering fees, latency, and available liquidity.
- Maintain a real-time view of best bid/ask across all connected exchanges.
- Factor in maker/taker fees: a slightly worse price on a maker-rebate venue may be cheaper net of fees.
- Split large orders across venues to minimize market impact.

### WebSocket Multiplexing and Management
- Use a centralized WebSocket manager that handles all connections, subscriptions, and reconnections.
- Implement subscription reference counting: if multiple consumers need the same stream, share one subscription.
- Buffer messages during reconnection. Detect and fill gaps by querying REST endpoints.
- Health dashboard: visualize the status of every WebSocket connection (connected, disconnected, reconnecting, stale).

## What NOT To Do

- Never store API keys in git repositories, even private ones. Use `.gitignore` and pre-commit hooks to prevent accidental commits.
- Never ignore rate limit headers. If an exchange returns `X-MBX-USED-WEIGHT`, track it and throttle proactively.
- Never retry order placement blindly. Always check order status first to avoid duplicate fills.
- Never use unlimited token approvals for DEX integration without understanding the risk. An exploited router contract can drain approved tokens.
- Never assume API response formats are stable. Validate response schemas and handle unexpected fields gracefully.
- Never use a single WebSocket connection for critical and non-critical data. A slow consumer on non-critical data can block critical data delivery.
- Never skip TLS certificate validation, even in development. Man-in-the-middle attacks on exchange APIs can steal keys and redirect funds.
- Never poll REST APIs at high frequency when a WebSocket alternative exists. REST polling wastes rate limit budget and increases latency.
- Never log API secrets, even in debug mode. Sanitize all log output to redact keys, secrets, and sensitive headers.
- Never treat exchange documentation as definitive. Test behavior empirically. Many exchanges have undocumented behaviors, rate limits, and quirks.