npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@xona-labs/x402-smart-layer

v1.0.1

Published

Smart validation layer middleware for x402 endpoints using AI-powered rule validation

Readme

🔒 x402-smart-layer

npm version Protocol

x402-smart-layer is an Express middleware that provides AI-powered request validation for x402 endpoints. It automatically validates incoming requests against expected structures using Gemini AI, ensuring payload integrity before processing.

✨ Features

  • 🤖 AI-Powered Validation: Uses Gemini AI to intelligently validate requests against flexible rules
  • 🎯 Route-Specific: Configure validation rules per endpoint
  • 🚀 Zero Config: Works out of the box with sensible defaults
  • 📦 TypeScript: Full TypeScript support with type definitions

📦 Installation

npm install @xona-labs/x402-smart-layer
# or
bun add @xona-labs/x402-smart-layer

🚀 Quick Start

import express from 'express';
import { createSmartLayer } from '@xona-labs/x402-smart-layer';
import { paymentMiddleware, x402ResourceServer } from '@x402/express';

const app = express();
app.use(express.json());

// Configure smart layer middleware
const smartLayer = createSmartLayer({
  debug: true, // Enable debug logging (optional)
  endpoints: {
    '/image/nano-banana': {
      expectedRequest: {
        prompt: 'must be a non-empty string describing the image',
        aspect_ratio: 'must be one of: 16:9, 9:16, 1:1',
        style: 'optional string for style preference'
      }
    }
  }
});

// Apply smart layer first (validates payload before payment)
app.use(smartLayer);

// Setup x402 resource server
const x402Server = new x402ResourceServer(/* ... */);

// Your protected route
app.post('/image/nano-banana', 
  paymentMiddleware({ resourceServer: x402Server, /* ... */ }),
  (req, res) => {
    // Request is validated and payment is settled
    res.json({ success: true, data: req.body });
  }
);

🛠 API Reference

createSmartLayer(config)

Creates Express middleware for smart validation.

Parameters

  • config (SmartLayerConfig):
    • endpoints (Record<string, EndpointConfig>): Map of route paths to their configurations
      • expectedRequest (Record<string, any>): Expected request structure
    • debug (boolean, optional): Enable debug logging. When true, logs detailed information about the validation process including route checks, payload validation, and API calls. Default: false

Returns

Express middleware function that:

  • Only processes requests to configured endpoints
  • Validates requests when a request body is present (runs before payment processing)
  • Returns 400 if validation fails
  • Attaches validation result to req.smartLayerValidation on success

📝 Example

See examples/x402-integration.ts for a complete working example with x402 endpoints.

Run the example:

npm install
npm install @x402/express @x402/core @x402/svm
npm run build
npm run example

Or directly:

npx ts-node --esm examples/x402-integration.ts

🔍 How It Works

  1. Route Matching: Middleware checks if the incoming request path matches a configured endpoint
  2. Request Body Detection: Validates requests that have a request body present
  3. Rule Generation: Generates validation rules from the expectedRequest structure
  4. AI Validation: Calls the smart layer API (powered by Gemini) to validate the payload
  5. Response: Returns 400 if validation fails, otherwise continues to next middleware

🔗 Integration with x402

The smart layer is designed to work seamlessly with @x402/express middleware. Apply the smart layer before x402 payment middleware to validate payloads before processing payments.

Important: The smart layer validates request payloads when a request body is present, regardless of payment headers. This ensures payload validation happens before payment processing, catching invalid requests early and preventing unnecessary payment processing.

See examples/x402-integration.ts for a complete example showing:

  • x402 resource server setup
  • Smart layer configuration
  • Protected route implementation
  • Proper middleware ordering

🛡 Security

  • Only validates requests with payment headers (x402 protocol requirement)
  • Validation happens before your route handlers
  • Failed validations return clear error messages

📄 License

MIT © Xona Labs