TypeScript SDK API reference
Every public function and type in @mitulabs/payments, with what each one does and when.
The package is split into a core and a set of framework adapters. The core, imported from @mitulabs/payments, contains the client and the shared types. Each adapter lives in its own subpath, such as @mitulabs/payments/express, so your application only loads the code for the framework you use. Every adapter is built on the same core method, mitu.admit(), which means they all behave identically.
For the options you can pass when creating a client, see Configuration. For the failures you may need to handle, see Errors.
Every adapter also re-exports the price and payment types, so you can import them from whichever path you are already using.
The client
createMitu
Creates a Mitu client from your options. The function validates the key and the default currency immediately and throws a MituConfigError if either is invalid. It does not make any network calls. Create a single client when your application starts and pass it to every route that charges.
mitu.admit
Decides whether a single request may go through. This is the method every adapter calls internally, and it is also how you add payments to a framework that has no adapter. It takes a standard Fetch API Request and the price for the route, and it returns an Admission describing what to do next.
- The request carries no payment.
The method asks Mitu to create one and returnsadmitted: falsewith a 402 Payment Required response. Send that response to the caller as it is. - The request carries a valid payment.
The payment is for this route and price, so Mitu settles it and the method returnsadmitted: truewith the payment and a set of receipt headers. Serve the request, then copy those headers onto your response. - The price is
null.
The request is treated as free. The method returnsadmitted: truewith no payment and does not contact Mitu.
The example below shows the complete pattern for a custom integration.
mitu.defaultCurrency
The default currency the client was created with, always in upper case.
Adapters
Each adapter provides a charge function that puts a route behind a payment. When you register a route with a fixed price, charge checks the price immediately, so an invalid amount stops your server at startup rather than failing on a live request.
Express
Returns Express middleware. Place it before your handler. Once the payment has settled, the middleware calls the next handler and makes the payment available on req.mitu.payment.
Hono
Returns Hono middleware. Type your application with MituEnv so that TypeScript knows about the payment, which your handler reads with c.get('mitu').payment.
Next.js App Router
Wraps a route handler. Your handler receives the request in the usual way, and its second argument contains the route params together with the settled payment.
Elysia
Returns hooks to pass to .guard(). Every route defined inside that guard is charged at the given price. Inside a handler, call mituOf(request) to read the payment.
Fetch API runtimes
Wraps a standard Fetch API handler, which makes it suitable for Cloudflare Workers, Deno and Bun. Your handler receives the request, then an object containing the payment, then any further arguments the runtime provides, such as env and ctx on Cloudflare Workers.
Prices
A price tells the SDK how much a route costs. You can give a fixed set of terms, null for a free route, or a function that decides the price for each request.
The examples below cover the common cases.
When you use a function, keep three rules in mind.
- It must return the same price twice.
The SDK calls the function on the first, unpaid request and again when the caller retries with payment. If the two answers differ, the payment will not match and the caller will be asked to pay again. - It can make a request free.
Returningnullserves that request without creating a payment. - It must never trust the request body.
Base the price on information you control, such as the caller’s plan or a stored order total.
Payment
When a request has been paid, your handler receives a Payment describing it. For a free request the payment is null.
Admission
The result of mitu.admit(). When admitted is true, serve the request and add the headers to your response, because they carry the payment receipt. When admitted is false, send the response to the caller unchanged. It will be a payment challenge, a refusal from Mitu, or a 503 while Mitu is unavailable.