Accept crypto payments in your Elysia API with the Armory middleware plugin.
Installation
# npm
npm install @armory-sh/middleware-elysia
# yarn
yarn add @armory-sh/middleware-elysia
# pnpm
pnpm add @armory-sh/middleware-elysia
# bun
bun add @armory-sh/middleware-elysia
Quick Start
import { Elysia } from 'elysia';
import { paymentMiddleware } from '@armory-sh/middleware-elysia';
const app = new Elysia()
.use(paymentMiddleware({
requirements: {
to: '0xYourWalletAddress...',
amount: 1000000n, // 1 USDC (6 decimals)
expiry: Date.now() + 3600 * 1000,
chainId: 'eip155:8453',
assetId: 'eip155:8453/erc20:0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'
}
}))
.get('/api/data', ({ store }) => {
return { message: 'Payment successful!' };
})
.listen(3000);
Accessing Payment Info
Payment information is stored in the Elysia store:
app.get('/api/premium', ({ store }) => {
const payment = store.payment;
return {
message: `Hello ${payment.payerAddress}`,
paid: payment.payload.amount
};
});
With Schema Validation
Combine payment middleware with Elysia’s schema validation:
import { t } from 'elysia';
app.post(
'/api/purchase',
({ store, body }) => {
const { itemId, quantity } = body;
const { payerAddress } = store.payment;
// Process purchase for verified payer
return { purchased: true, for: payerAddress };
},
{
body: t.Object({
itemId: t.String(),
quantity: t.Optional(t.Number()),
}),
}
);
Type Safety
For full TypeScript support, type your Elysia instance:
import type { PaymentContext } from '@armory-sh/middleware-elysia';
const app = new Elysia<{ store: PaymentContext }>()
.use(paymentMiddleware({ ... }))
.get('/api/data', ({ store }) => {
// store.payment is fully typed
console.log(store.payment.payerAddress);
});
Route Configuration
For per-route requirements, use routeAwarePaymentMiddleware:
import { routeAwarePaymentMiddleware } from '@armory-sh/middleware-elysia';
app.use(routeAwarePaymentMiddleware({
'/api/basic': { requirements: basicRequirements },
'/api/premium': { requirements: premiumRequirements },
}));
Tips
USDC on Base uses 6 decimals. 1 USDC = 1000000n as a bigint.
Elysia’s store is shared across all middleware. Payment info is available in any route after the middleware runs.
See @armory-sh/middleware-elysia for full configuration options.