Install the Mollie API client
masterYou can install the Mollie Node.js API client using npm or yarn.
npm install @mollie/api-clientor
yarn add @mollie/api-clientrepository·master·Indexed 43 days ago
https://github.com/mollie/mollie-api-nodeOfficial Node.js SDK for the Mollie API (v4.6.0), enabling developers to accept payments, manage refunds, and handle webhooks. Supports Node.js, Bun, Deno, and Cloudflare Workers. Features include async iterators for pagination, OAuth access token support, and comprehensive migration guides from v1.x.x through v4.0.0.
You can install the Mollie Node.js API client using npm or yarn.
npm install @mollie/api-clientor
yarn add @mollie/api-clientWhen upgrading to v4.0.0, several breaking changes must be addressed:
Node.js 14+ is officially supported.
withParent: Instead of using .withParent(parent), pass the parent ID directly in the options object to the iteration method.payments_refunds) have been removed. Use camel case (e.g., paymentRefunds) instead.mollieClient.payments.cancel instead of mollieClient.payments.delete..isOpen()) have been removed. Use direct property access and enums (e.g., if (payment.status == PaymentStatus.open)).ApiError Properties: Access error details via properties instead of methods. Use error.message, error.field, and error.statusCode instead of getMessage(), getField(), and getStatusCode().metadata Type: The metadata property is now typed as unknown. You must check its type at runtime or use as any to bypass type checking.count property on pages has been removed; use .length instead.mollieClient.methods.list), while others return iterators (e.g., customer.getPayments()).toPlainObject: This method has been removed.adapter, proxy, socketPath, and timeout passed to createMollieClient are ignored.Note that network error message strings may differ from previous versions. Update error handling that relies on exact message matching.
- const payments = mollieClient.customerPayments.withParent(customer).iterate();
+ const payments = mollieClient.customerPayments.iterate({ customerId: customer.id });
for await (const payment of payments) {
…
}Upgrading from v1.x.x to v2.0.0 involves significant changes due to the transition to the v2 Mollie API.
The NPM package was renamed to @mollie/api-client.
The client is now initialized via a factory function rather than a constructor:
const mollie = require('@mollie/api-client')({
apiKey: 'YOUR_API_KEY',
});(response) => {} to (error, response) => {}.currency and value (e.g., { currency: 'EUR', value: '10.00' }).createdAt now follow strict ISO-8601 formatting.cancelled was renamed to canceled. Statuses paidout, refunded, and charged_back were removed.recurringType was renamed to sequenceType.limit and from instead of offsets.withParent Deprecation: Use parent IDs in parameters instead of the .withParent() method.// New amount format
mollie.payments.create({
amount: {
"currency": "EUR",
"value": "10.00"
}
});
// New pagination pattern
mollie.payments
.all({
limit: 15,
from: 'tr_8WhJKGmgBy'
})
.then((payments) => {
// Returns the list of 15 payments, starting with payment `tr_8WhJKGmgBy`
});When upgrading to v3.0.0, the primary change is the transition to a named factory function for client initialization.
Use createMollieClient from the package:
const { createMollieClient } = require('@mollie/api-client');
const mollieClient = createMollieClient({ apiKey: 'YOUR_API_KEY' });import { createMollieClient, PaymentMethod } from '@mollie/api-client';
const mollieClient = createMollieClient({
apiKey: 'YOUR_API_KEY',
});const { createMollieClient, PaymentMethod } = require('@mollie/api-client');
const mollieClient = createMollieClient({
apiKey: 'test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM',
});
mollieClient.payments.create({
amount: {
currency: 'EUR',
value: '10.00',
},
description: 'Order #478',
method: PaymentMethod.ideal
});To use the library, create a client instance using createMollieClient. You can use either ES Modules or CommonJS syntax. You must provide an apiKey for authentication.
import createMollieClient from '@mollie/api-client';
const mollieClient = createMollieClient({ apiKey: 'test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM' });The details property in PaymentData provides specialized information depending on the method used. Common detail structures include:
cardHolder, cardNumber (last 4 digits), cardFingerprint, cardLabel, and failureReason for failed payments.consumerName, consumerAccount (IBAN), and consumerBic.consumerName, consumerAccount (email), paypalReference, and sellerProtection status.bankName, bankAccount (IBAN), bankBic, and transferReference.consumerName, consumerAccount, and qrCode for mobile scanning.The PaymentData interface represents the core data object for a Mollie payment. It contains information about the payment's status, amount, method, and lifecycle timestamps.
Key properties include:
mode: Indicates if the payment is live or test.status: The current state of the payment (e.g., authorized, paid, canceled).amount: An object containing the currency and value.method: The payment method used (e.g., ideal, creditcard, paypal).statusReason: Details about why a payment reached a specific status (primarily for point-of-sale)._links: A collection of URLs for interacting with the payment (e.g., checkout, dashboard, refunds).details: A polymorphic object containing method-specific information (e.g., CreditCardDetails, IdealDetails).When fetching capture data, you can embed related resources using the embed query parameter. Use the CaptureEmbed enum to specify which resources to include.
Note: The CaptureInclude enum is deprecated. Use CaptureEmbed instead. Passing include to the Captures API has no effect.
// Use CaptureEmbed to include related resources like the payment
// Example usage in an API call (conceptual):
// client.captures.get('cap_123', { embed: CaptureEmbed.payment });By default, the client throws an error if it detects a browser-like environment to prevent accidental credential exposure. If you have appropriate mitigations in place (such as using a short-lived, narrowly-scoped access token), you can bypass this check using the dangerouslyAllowBrowser: true option.
const mollieClient = createMollieClient({ apiKey: 'test_...', dangerouslyAllowBrowser: true });The Mollie API returns results in pages. To implement manual pagination, use the .page() method. This method returns a subset of objects and provides a nextPageCursor which is the ID of the first object on the next page. You can use this cursor in the from parameter of the subsequent call.
// Retrieve the first 15 payments.
const payments = mollieClient.payments.page({ limit: 15 });
// payments.nextPageCursor is the cursor: the ID of the first payment on the next page.
// Retrieve the second 15 payments (using the cursor from the previous page).
const nextPayments = mollieClient.payments.page({ limit: 15, from: 'tr_8WhJKGmgBy' });Retrieve the current status of an existing payment using its ID with mollieClient.payments.get().
const payment = await mollieClient.payments.get('tr_8WhJKGmgBy');
// Check payment.status.Use mollieClient.payments.create() to initiate a new payment. You must provide the amount (with value and currency), a description, a redirectUrl (where the customer is sent after payment), and a webhookUrl (where Mollie sends status updates).
const payment = await mollieClient.payments.create({
amount: {
value: '10.00',
currency: 'EUR'
},
description: 'My first API payment',
redirectUrl: 'https://yourwebshop.example.org/order/123456',
webhookUrl: 'https://yourwebshop.example.org/webhook'
});
// Forward the customer to payment.getCheckoutUrl().