MercadoPago plugin for Better Auth β subscriptions, payments & webhooks out of the box.
β οΈ Disclaimer: This plugin is community-maintained and is not officially affiliated with MercadoPago or Better Auth. See DISCLAIMER.md for full details.
- π Authentication-first payments β Payments tied to authenticated Better Auth users
- π³ Subscription management β Create and manage recurring subscriptions with MercadoPago
- π Webhook handling β Built-in webhook verification and event processing
- π¦ Type-safe β Full TypeScript support with Zod validation
- π Pluggable β Drop-in Better Auth plugin with minimal configuration
- π LATAM-ready β Built specifically for MercadoPago's Latin American market
# npm
npm install better-auth-mp
# yarn
yarn add better-auth-mp
# pnpm
pnpm add better-auth-mp
# bun
bun add better-auth-mpIf you use an AI coding agent (Claude Code, Antigravity, Codex, Cursor, OpenCode...) install the
better-auth-mp skill so your agent knows how to integrate this plugin without extra prompting:
# npx
npx skills add IvanTsxx/better-auth-mp
# bunx
bunx --bun skills add IvanTsxx/better-auth-mp
# pnpm
pnpm dlx skills add IvanTsxx/better-auth-mpCompatible with 40+ AI coding agents.
Make sure you have the required peer dependencies installed:
bun add better-auth @prisma/clientAdd the plugin to your Better Auth configuration:
// auth.ts (server)
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { mercadopago } from "better-auth-mp";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
plugins: [
mercadopago({
accessToken: process.env.MP_ACCESS_TOKEN!,
webhookSecret: process.env.MP_WEBHOOK_SECRET!,
}),
],
});Add the client plugin to your Better Auth client:
// auth-client.ts (client)
import { createAuthClient } from "better-auth/react";
import { mercadopagoClient } from "better-auth-mp/client";
export const authClient = createAuthClient({
plugins: [mercadopagoClient()],
});# MercadoPago credentials
MP_ACCESS_TOKEN=APP_USR-xxxxxxxxxxxxxxxxxxxx
MP_WEBHOOK_SECRET=your-webhook-secret
# Better Auth
BETTER_AUTH_SECRET=your-auth-secret
BETTER_AUTH_URL=http://localhost:3000| Option | Type | Required | Description |
|---|---|---|---|
accessToken |
string |
β | MercadoPago Access Token (production or sandbox) |
webhookSecret |
string |
β | Secret for webhook signature verification |
sandbox |
boolean |
β | Enable sandbox mode (default: false) |
notificationUrl |
string |
β | Override webhook URL (default: auto-detected from BETTER_AUTH_URL) |
mercadopago({
accessToken: process.env.MP_ACCESS_TOKEN!,
webhookSecret: process.env.MP_WEBHOOK_SECRET!,
sandbox: process.env.NODE_ENV !== "production",
notificationUrl: "https://yourdomain.com/api/auth/mercadopago/webhook",
})This plugin is fully ORM-agnostic β it works with Prisma, Drizzle, or any other ORM/database supported by Better Auth.
The required schema is generated automatically using the Better Auth CLI. Run it once after configuring the plugin:
# bun
bunx @better-auth/cli generate
# npm
npx @better-auth/cli@latest generate
# pnpm
pnpm dlx @better-auth/cli@latest generate
# yarn
yarn dlx @better-auth/cli@latest generateThe CLI reads your auth.ts configuration and generates the necessary models/migrations for your configured ORM and database automatically.
Create the webhook route handler:
// app/api/auth/[...betterauth]/route.ts
import { auth } from "@/lib/auth";
import { toNextJsHandler } from "better-auth/next-js";
export const { POST, GET } = toNextJsHandler(auth);The plugin automatically registers the webhook endpoint at /api/auth/mercadopago/webhook.
The plugin handles the following MercadoPago webhook events:
| Event | Description |
|---|---|
subscription_preapproval |
Subscription created or updated |
payment |
Payment received, refunded, or failed |
import { authClient } from "@/lib/auth-client";
// In your component
const { data: session } = authClient.useSession();
const subscription = authClient.mercadopago.useSubscription();| Feature | Status |
|---|---|
| β Basic payment webhooks | Released |
| β Subscription management | Released |
| β Webhook signature verification | Released |
| π One-click checkout via Brick | In progress |
| π Subscription plan management UI | In progress |
| π Refund management | Planned |
| π Payment analytics endpoints | Planned |
| π Multi-currency support | Planned |
| π Installment (cuotas) support | Planned |
We welcome contributions! Please read our CONTRIBUTING.md guide before submitting a PR.
MIT β Copyright Β© 2025 better-auth-mp contributors.
See DISCLAIMER.md for important legal information.