Zion
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/sdk
pnpm add @ziongateway/sdk
yarn add @ziongateway/sdk
bun add @ziongateway/sdk

Configuration

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

OptionTypeRequiredDescription
apiKeystringYour Zion API Key from the Dashboard
pricenumberCost per request in USDC
networkstringsolana-mainnet or solana-devnet
resourcestringLabel 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).

app/api/paid-route/route.ts
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

  1. Request Received - Client tries to access a protected route
  2. No Payment Header - SDK returns 402 Payment Required with payment details
  3. Client Pays - Client creates atomic split transaction and gets signature
  4. Retry with Header - Client sends request with Authorization: Bearer <payment_header>
  5. Verification - SDK verifies payment with Zion Facilitator
  6. Settlement - Payment is settled and your handler executes

On this page