{"id":18044,"library":"midtrans-client","title":"Midtrans Payment API Client for Node.js","description":"The `midtrans-client` package is the official Node.js API client for integrating with the Midtrans Payment Gateway. It provides a structured interface for interacting with Midtrans' two primary payment products: Snap, which offers a customizable payment popup directly on the merchant's website, and Core API (VT-Direct), which facilitates backend-driven payment flows with full frontend customization. The library is currently at version 1.4.3, with recent updates addressing dependency vulnerabilities and introducing new features such as Snap Bi functionalities (Direct Debit, QRIS, VA) and webhook notification verification. Its release cadence indicates active maintenance, with focus on reliability and feature expansion. As the official client, it provides a well-supported and up-to-date integration path, adhering to Midtrans' specific API requirements and product offerings.","status":"active","version":"1.4.3","language":"javascript","source_language":"en","source_url":"https://github.com/midtrans/midtrans-nodejs-client","tags":["javascript","payment","gateway","api","midtrans","node"],"install":[{"cmd":"npm install midtrans-client","lang":"bash","label":"npm"},{"cmd":"yarn add midtrans-client","lang":"bash","label":"yarn"},{"cmd":"pnpm add midtrans-client","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary usage pattern shown in documentation and examples is CommonJS `require`. While ES module `import` might work with bundlers or Node.js's ES module support, `require` is the officially documented and most compatible method for this package.","wrong":"import midtransClient from 'midtrans-client';","symbol":"midtransClient","correct":"const midtransClient = require('midtrans-client');"},{"note":"The `CoreApi` class is a property of the main `midtransClient` object. Direct named imports are not explicitly documented and may not function as expected due to the CommonJS export structure.","wrong":"import { CoreApi } from 'midtrans-client';","symbol":"CoreApi","correct":"const midtransClient = require('midtrans-client');\nconst coreApi = new midtransClient.CoreApi({ /* config */ });"},{"note":"The `Snap` class is a property of the main `midtransClient` object. Direct named imports are not explicitly documented and may not function as expected due to the CommonJS export structure.","wrong":"import { Snap } from 'midtrans-client';","symbol":"Snap","correct":"const midtransClient = require('midtrans-client');\nconst snap = new midtransClient.Snap({ /* config */ });"}],"quickstart":{"code":"const midtransClient = require('midtrans-client');\n\n// Initialize the Snap API client with environment variables for sensitive keys\nconst snap = new midtransClient.Snap({\n    isProduction: process.env.MIDTRANS_IS_PRODUCTION === 'true',\n    serverKey: process.env.MIDTRANS_SERVER_KEY ?? '',\n    clientKey: process.env.MIDTRANS_CLIENT_KEY ?? ''\n});\n\n// Define transaction parameters as per Midtrans Snap API documentation\nconst parameter = {\n    transaction_details: {\n        order_id: `ORDER-${Date.now()}`,\n        gross_amount: 10000 // Example: 10,000 IDR\n    },\n    credit_card: {\n        secure: true // Ensure 3D Secure is enabled\n    },\n    customer_details: {\n        first_name: 'Budi',\n        last_name: 'Pratama',\n        email: 'budi.pratama@example.com',\n        phone: '08123456789'\n    }\n};\n\n// Create the transaction and handle the response\nsnap.createTransaction(parameter)\n    .then((transaction) => {\n        // The transaction object contains token and redirect_url\n        let transactionToken = transaction.token;\n        console.log('Snap Transaction Token:', transactionToken);\n        let redirectUrl = transaction.redirect_url;\n        console.log('Snap Redirect URL:', redirectUrl);\n        \n        // In a real application, you would send this token or redirectUrl to your frontend\n        // to initiate the Snap payment popup or redirect the user.\n    })\n    .catch((e) => {\n        console.error('Error creating Snap transaction:', e.message);\n        // Handle error, e.g., log it or return an error response to the client\n    });\n","lang":"javascript","description":"This example demonstrates how to initialize the Midtrans Snap API client and create a new payment transaction, retrieving the transaction token and redirect URL."},"warnings":[{"fix":"Upgrade to `midtrans-client@1.4.2` or a newer version immediately to resolve the 'SnapBiApiRequestor' import problem and ensure the correct operation of Snap Bi features.","message":"Version 1.4.2 addressed a critical breaking issue where the module could not properly locate 'SnapBiApiRequestor'. This likely caused runtime errors for users attempting to utilize Snap Bi functionalities prior to this fix.","severity":"breaking","affected_versions":"<1.4.2"},{"fix":"Adhere to `const midtransClient = require('midtrans-client');` for maximal compatibility. If ESM is required, ensure your project's `package.json` specifies `\"type\": \"module\"` and/or use a bundler (like Webpack, Rollup) or transpiler (like Babel, TypeScript).","message":"The documentation primarily showcases CommonJS `require()` syntax. Attempting to use ES module `import` statements directly might lead to 'Cannot find module' or 'Named export not found' errors in Node.js environments not configured for ESM or without appropriate transpilation.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always cross-reference your API keys with the chosen environment in your Midtrans Dashboard. Set `isProduction: true` for live transactions and `false` for testing/sandbox environments, matching your provided keys.","message":"Misconfiguration of `isProduction` or providing incorrect `serverKey`/`clientKey` will result in authentication failures or transactions being processed in the unintended environment (sandbox instead of production, or vice-versa).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Regularly update the `midtrans-client` package to its latest stable version to incorporate security patches and benefit from updated dependencies.","message":"Version 1.4.3 included crucial dependency updates to address reported vulnerability warnings. Running older versions of the library may expose your application to known security vulnerabilities originating from its transitive dependencies.","severity":"gotcha","affected_versions":"<1.4.3"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Upgrade your `midtrans-client` package to version `1.4.2` or higher. This particular issue was fixed in that release.","cause":"This error specifically occurred in versions prior to 1.4.2 due to an internal module resolution issue when attempting to access `SnapBiApiRequestor` for Snap Bi related functionalities.","error":"ReferenceError: SnapBiApiRequestor is not defined"},{"fix":"Verify that your `serverKey` and `clientKey` are correct and correspond to the `isProduction` setting in your client initialization. Ensure you are not mixing sandbox and production keys/environments.","cause":"This typically indicates an authentication failure with the Midtrans API, most commonly due to invalid `serverKey` or `clientKey`, or using keys meant for one environment (e.g., sandbox) in another (e.g., production).","error":"Error: Unauthorized. Access denied for this request."},{"fix":"Ensure the package is installed by running `npm install midtrans-client`. If you're manually including the library, verify the `require()` path is correct, e.g., `require('./path/to/midtrans-client-nodejs/index.js')`.","cause":"The package `midtrans-client` was either not installed in your project, or Node.js cannot resolve its path.","error":"Error: Cannot find module 'midtrans-client'"},{"fix":"Refactor your import to use CommonJS `require()` and access `Snap` as a property: `const midtransClient = require('midtrans-client'); let snap = new midtransClient.Snap(...);`","cause":"This error occurs when using ES module `import { Snap } from 'midtrans-client';` syntax, but the package primarily exposes its classes as properties of a default or module export using CommonJS.","error":"SyntaxError: Named export 'Snap' not found. The requested module 'midtrans-client' does not provide an export named 'Snap'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}