> ## Documentation Index
> Fetch the complete documentation index at: https://armory.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# With Elysia

> Accept Armory payments in Elysia

Accept crypto payments in your Elysia API with the Armory middleware plugin.

## Installation

```bash theme={null}
# 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

```typescript theme={null}
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:

```typescript theme={null}
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:

```typescript theme={null}
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:

```typescript theme={null}
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`:

```typescript theme={null}
import { routeAwarePaymentMiddleware } from '@armory-sh/middleware-elysia';

app.use(routeAwarePaymentMiddleware({
  '/api/basic': { requirements: basicRequirements },
  '/api/premium': { requirements: premiumRequirements },
}));
```

***

## Tips

<Tip>
  USDC on Base uses 6 decimals. 1 USDC = `1000000n` as a bigint.
</Tip>

<Note>
  Elysia's store is shared across all middleware. Payment info is available in any route after the middleware runs.
</Note>

***

## More Information

See [`@armory-sh/middleware-elysia`](/packages/middleware-elysia) for full configuration options.
