@codex-data/sdk
v2.0.1
Published
The Codex SDK for JavaScript/Typescript. It provides generated types and convenient ways to access the graphql api.
Readme
Codex SDK
This package exists to help you develop on top of the Codex API (https://docs.codex.io).
It provides generated TypeScript types and convenient methods to access the GraphQL API with full type safety.
[!NOTE] We've changed our name from Defined to Codex.
You will see references to our previous company name, Defined, while we make the switch to Codex.
Features
- 🚀 Fully Typed: Generated TypeScript types for all GraphQL operations
- 📦 Tree Shakeable: ESM support with optimized bundle size
- 🔄 Real-time: WebSocket subscriptions support
- 🛠 Developer Friendly: Comprehensive examples and documentation
Installation
| Package Manager | Command |
| ----------------------------- | ----------------------------- |
| npm | npm install @codex-data/sdk |
| yarn | yarn add @codex-data/sdk |
| pnpm | pnpm add @codex-data/sdk |
Usage
Follow one of the examples in the examples directory, or simply run.
Fetch a token.
import { Codex } from "@codex-data/sdk";
const sdk = new Codex(MY_API_KEY);
sdk.queries
.token({
input: {
address: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
networkId: 56,
},
})
.then(console.log);Use your own GraphQL selections
import { Codex } from "@codex-data/sdk";
const sdk = new Codex(process.env.CODEX_API_KEY || "");
// Using the raw GraphQL client
sdk
.send<{ getNetworks: Array<{ id: string; name: string }> }>(
`
query GetNetworks {
getNetworks { id name }
}
`,
{},
)
.then((res) => {
console.log("Networks: ", res.getNetworks);
});Subscribe to real-time data
import { Codex } from "@codex-data/sdk";
const sdk = new Codex(process.env.CODEX_API_KEY || "");
// Subscribe to price updates
sdk.subscriptions.onPriceUpdated(
{
address: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c",
networkId: 56,
},
(result) => {
if (result.data) {
console.log("Price updated:", result.data.onPriceUpdated);
}
},
);Development
Prerequisites
Building from Source
# Install dependencies
pnpm install
# Build the SDK
pnpm run buildRunning Examples
All examples require a Codex API key. Get your API key at docs.codex.io.
Simple Example
Basic usage with inline GraphQL queries:
cd examples/simple
pnpm install
CODEX_API_KEY=your_api_key pnpm run devCodegen Example
Shows how to use GraphQL Code Generator for fully typed queries:
cd examples/codegen
pnpm install
pnpm run codegen
CODEX_API_KEY=your_api_key pnpm run devNext.js Example
Full-stack example with a Next.js application:
cd examples/next
pnpm install
NEXT_PUBLIC_CODEX_API_KEY=your_api_key pnpm run devPackage Structure
The SDK is built with modern tooling and provides both CommonJS and ESM builds:
@codex-data/sdk/
├── dist/
│ ├── index.js # CommonJS entry point
│ ├── index.mjs # ESM entry point
│ ├── index.d.ts # TypeScript definitions
│ └── sdk/ # SDK implementation
├── README.md
└── package.jsonContributing
We welcome contributions! Please feel free to submit a Pull Request.
Development Scripts
pnpm run build- Build the SDK for productionpnpm run test- Run the test suitepnpm run lint- Lint the codebasepnpm run clean- Clean build artifacts
Releasing
Publishing Beta Versions
For testing releases before making them public:
Update the version in
package.jsonto a beta version:"version": "2.0.0-beta.0"Publish to the beta tag:
pnpm run publish:betaTest the beta version:
npm install @codex-data/sdk@beta
Users installing without the @beta tag will continue to receive the latest stable version.
Publishing Production Releases
Update the version in
package.jsonto a production version:"version": "2.0.0"Publish to the latest tag:
pnpm run publish:latest
Note: The
prepublishOnlyscript automatically runs the full build before publishing, ensuring the package is always built before release.
Upgrading to 2.0.0+
There are a few breaking changes in 2.0.0 that you need to be aware of.
Imports
Before
import { Codex } from "@codex-data/sdk";
import { TokenRankingAttribute, RankingDirection } from "@codex-data/sdk/dist/sdk/generated/graphql";After
import { Codex, TokenRankingAttribute, RankingDirection } from "@codex-data/sdk";Before
import { Codex, GraphQL } from "@codex-data/sdk";
const [quoteToken, setQuoteToken] = useState<GraphQL.QuoteToken>(GraphQL.QuoteToken.Token0);After
import { Codex, QuoteToken } from "@codex-data/sdk";
const [quoteToken, setQuoteToken] = useState<QuoteToken>(QuoteToken.Token0);onLaunchpadTokenEventBatch
Before
const unsubscribeFn = codex.subscriptions.onLaunchpadTokenEventBatch(
{
networkId: networkId
}
)After
const unsubscribeFn = codex.subscriptions.onLaunchpadTokenEventBatch(
{
input: {
networkId: networkId
}
}
)