This directory contains comprehensive examples demonstrating how to use the Codex SDK in various scenarios. Each example is self-contained and showcases different aspects of the SDK functionality.
- Node.js >= 17.5.0
- pnpm (recommended)
- A Codex API key from docs.codex.io
-
Install dependencies (from the root directory):
pnpm install
-
Set your API key:
export CODEX_API_KEY="your_api_key_here"
-
Run any example:
cd examples/simple pnpm run dev
📁 simple/
Basic SDK Usage Examples
A collection of focused examples demonstrating core SDK functionality:
| File | Description | Key Features |
|---|---|---|
index.ts |
Complete SDK overview | Queries, mutations, subscriptions, webhooks |
token.ts |
Token data queries | Custom headers, GraphQL queries |
filterTest.ts |
Token filtering | Advanced filtering, liquidity checks |
realtime.ts |
Real-time price updates | WebSocket subscriptions, custom sink |
simpleSubscription.ts |
Basic subscriptions | Built-in subscription methods |
apiKeys.ts |
API token management | Token creation, short-lived tokens |
apiKeysSubscription.ts |
Token + subscriptions | Token auth with real-time data |
updateConfig.ts |
Dynamic configuration | Runtime config updates |
profile.ts |
Event profiling | Unconfirmed events, timing analysis |
auto.ts |
Automated queries | Simple token lookups |
Usage:
cd examples/simple
pnpm run dev # Run index.ts
tsx token.ts # Run specific example
CODEX_API_KEY=xyz tsx auto.ts # With inline API key📁 codegen/
GraphQL Code Generation
Shows how to use GraphQL Code Generator for fully typed queries and mutations.
Features:
- Type-safe GraphQL operations
- Generated TypeScript types
- Custom document nodes
- Full IntelliSense support
Usage:
cd examples/codegen
pnpm run codegen # Generate types
pnpm run dev # Run exampleKey Files:
codegen.ts- Code generation configurationsrc/index.ts- Typed query examplesrc/gql/- Generated types and utilities
📁 next/
Next.js Integration
A complete Next.js application demonstrating SDK usage in a React environment.
Features:
- Server-side rendering with SDK
- Client-side data fetching
- Real-time updates
- Modern UI components
- Network and token exploration
Usage:
cd examples/next
pnpm run dev # Start development server
pnpm run build # Build for productionKey Features:
- 🌐 Network browsing
- 🪙 Token exploration
- 📊 Price charts
- 🔄 Real-time updates
- 📱 Responsive design
The Codex SDK provides full TypeScript support with generated types for all GraphQL operations:
import {
Codex,
TokenQuery,
OnPriceUpdatedSubscription,
CreateWebhooksMutation,
} from "@codex-data/sdk";
// All operations are fully typed
const token: TokenQuery = await sdk.queries.token({
input: { address, networkId },
});- Query Types:
TokenQuery,GetNetworksQuery, etc. - Mutation Types:
CreateWebhooksMutation,DeleteWebhooksMutation, etc. - Subscription Types:
OnPriceUpdatedSubscription,OnTokenEventsCreatedSubscription, etc. - Input Types:
TokenInput,CreateWebhooksInput, etc. - Scalar Types: All GraphQL scalars and custom types
import { Codex } from "@codex-data/sdk";
const sdk = new Codex(process.env.CODEX_API_KEY || "");
const token = await sdk.queries.token({
input: {
address: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
networkId: 56,
},
});sdk.subscriptions.onPriceUpdated(
{
address: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
networkId: 56,
},
{
next: (data) => console.log("Price update:", data),
error: (err) => console.error("Error:", err),
complete: () => console.log("Subscription complete"),
},
);Custom queries let you request only the fields you need — you're billed for the fields your query requests, so this is the recommended pattern for production. Wrap the SDK's exported query types in DeepPartial so the compiler stays honest about fields your query didn't select:
import { DeepPartial, GetNetworksQuery } from "@codex-data/sdk";
const result = await sdk.send<DeepPartial<GetNetworksQuery>>(
`query GetNetworks { getNetworks { id name } }`,
{},
);For exact types inferred from your query (no DeepPartial needed), see the codegen example.
const webhook = await sdk.mutations.createWebhooks({
input: {
priceWebhooksInput: {
webhooks: [
{
name: "Price Alert",
callbackUrl: "https://your-webhook-url.com",
conditions: {
priceUsd: { gt: "100" },
networkId: { eq: 1 },
},
},
],
},
},
});All examples support these environment variables:
| Variable | Description | Default |
|---|---|---|
CODEX_API_KEY |
Your Codex API key | Required |
CODEX_API_URL |
Custom API endpoint | https://graph.codex.io/graphql |
CODEX_WS_URL |
Custom WebSocket endpoint | wss://graph.codex.io/graphql |
Each example directory includes these scripts:
pnpm run dev- Run the main example filepnpm run codegen- Generate GraphQL types (codegen example only)pnpm run build- Build for production (Next.js example only)
- Store API keys in environment variables
- Use
.env.localfiles for local development - Never commit API keys to version control
- Use subscriptions for real-time data
- Implement proper error handling
- Consider rate limiting for production use
- Use TypeScript for better development experience
- Leverage code generation for type safety
- Test with different network configurations
- Handle subscription lifecycle properly
- Implement reconnection logic for production
- Use appropriate cleanup mechanisms
"API key not found"
export CODEX_API_KEY="your_api_key_here""Module not found"
pnpm install # From project root"WebSocket connection failed"
- Check your network connection
- Verify WebSocket URL is correct
- Ensure API key has subscription permissions
Found a bug or want to add an example? We welcome contributions!
- Fork the repository
- Create your feature branch
- Add your example with proper documentation
- Submit a pull request
Happy coding with Codex SDK! 🚀