SDK Reference
JavaScript / TypeScript
Integrate Zion into your Node.js application.
Server-Side Integration
The ZionGate class provides middleware adapters for popular JavaScript/TypeScript frameworks.
Installation
npm install @ziongateway/sdkpnpm add @ziongateway/sdkyarn add @ziongateway/sdkbun add @ziongateway/sdkConfiguration
Initialize ZionGate with your configuration:
import { ZionGate } from "@ziongateway/sdk";
const zion = new ZionGate({
apiKey: "YOUR_DEVELOPER_API_KEY", // Get this from Zion Dashboard
price: 0.1, // Price per request in USDC
network: "solana-devnet", // or "solana-mainnet"
resource: "ai-agent-api" // Optional label (default: "api-access")
});Configuration Options
| Option | Type | Required | Description |
|---|---|---|---|
apiKey | string | ✅ | Your Zion API Key from the Dashboard |
price | number | ✅ | Cost per request in USDC |
network | string | ✅ | solana-mainnet or solana-devnet |
resource | string | ❌ | Label for the resource (default: api-access) |
Framework Integration
Use the .express() method to create standard Express middleware.
import express from "express";
import { ZionGate } from "@ziongateway/sdk";
const app = express();
const zion = new ZionGate({
apiKey: "YOUR_DEVELOPER_API_KEY",
price: 0.1,
network: "solana-devnet",
resource: "ai-agent-api"
});
// Protect a specific route
app.get("/ai-agent-api", zion.express(), (req, res) => {
res.json({
success: true,
message: "You have paid for this content! 🚀",
data: "SECRET_DATA"
});
});
app.listen(3000, () => console.log("Server running on port 3000"));Use the nextServer helper in your Route Handlers (App Router).
import { ZionGate } from "@ziongateway/sdk";
const zion = new ZionGate({
apiKey: process.env.ZION_API_KEY!,
price: 0.5,
network: "solana-devnet",
resource: "premium-api"
});
export async function GET(req: Request) {
// Check authorization
const authorized = await zion.nextServer(req);
// If not authorized, 'authorized' is a 402 Response object
if (authorized !== true) {
return authorized;
}
// Serve premium content
return Response.json({
success: true,
data: "Paid Content"
});
}[!NOTE] Support for Next.js Edge Middleware is coming soon.
Use ZionGate in a functional middleware or implement NestMiddleware.
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { ZionGate } from '@ziongateway/sdk';
@Injectable()
export class ZionMiddleware implements NestMiddleware {
private zion = new ZionGate({
apiKey: process.env.ZION_API_KEY!,
price: 0.1,
network: 'solana-devnet',
resource: 'premium-api'
});
use(req: Request, res: Response, next: NextFunction) {
const middleware = this.zion.express();
middleware(req, res, next);
}
}Apply to routes in your module:
import { Module, MiddlewareConsumer } from '@nestjs/common';
import { ZionMiddleware } from './zion.middleware';
@Module({})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(ZionMiddleware)
.forRoutes('premium/*');
}
}[!NOTE] Native NestJS Guard implementation coming soon.
How It Works
- Request Received - Client tries to access a protected route
- No Payment Header - SDK returns
402 Payment Requiredwith payment details - Client Pays - Client creates atomic split transaction and gets signature
- Retry with Header - Client sends request with
Authorization: Bearer <payment_header> - Verification - SDK verifies payment with Zion Facilitator
- Settlement - Payment is settled and your handler executes