Getting started
This page takes you through a first sandbox payment: reserve it, let the user approve it, confirm it. The complete version you can adapt for production is in the recipes.
1. Get sandbox credentials
Use the LINE Pay sandbox for development and tests, never your production keys.
- Apply for a sandbox account on the official sandbox page.
- Sign in to the Merchant Center and find your Channel ID and Channel Secret Key on the "Manage link key" page.
Put them in environment variables, not in the code, and certainly not in Git:
export LINE_PAY_CHANNEL_ID=...
export LINE_PAY_CHANNEL_SECRET=...2. Install
npm install line-pay-merchantpnpm add line-pay-merchantyarn add line-pay-merchantNode.js 22 or later is required. The types are included; there is no @types package to install.
3. Create a client
import { createLinePayClient } from 'line-pay-merchant'
export const linePay = createLinePayClient({
channelId: process.env.LINE_PAY_CHANNEL_ID ?? '',
channelSecretKey: process.env.LINE_PAY_CHANNEL_SECRET ?? '',
env: 'sandbox' // 'production' for real payments
})- Creating a client does not connect to anything and keeps no global state. One per process is enough, and it can be shared safely.
envis'sandbox'or'production'. ('development'of 0.9 was renamed to'sandbox'.)- A bad configuration (a missing key, a mistyped
env) throws aLinePayValidationErrorwhen the client is created, not at the first payment.
4. Reserve a payment: request
export async function reservePayment() {
const { body } = await linePay.request({
amount: 1000,
currency: 'TWD',
// A new one for every try: LINE Pay refuses an order ID it has seen.
orderId: `order-${crypto.randomUUID()}`,
packages: [
{
id: 'package-1',
amount: 1000,
products: [{ name: 'Tea', quantity: 2, price: 500 }]
}
],
redirectUrls: {
confirmUrl: 'https://shop.example/line-pay/confirm',
cancelUrl: 'https://shop.example/line-pay/cancel'
}
})
// Send the user to body.info.paymentUrl.web (or .app on a phone).
// Keep body.info.transactionId: you need it to confirm.
return body.info
}amountmust equal the sum of allpackages[].amount(plus fees and shipping), and theamountof each package must equal the sum ofquantity × priceof its products. The library checks this before sending and throws aLinePayValidationErrorfor a wrong sum, so no request is wasted.orderIdis your order number and must be unique in your system.body.info.transactionIdis LINE Pay's transaction ID. Confirm, refund and reconcile all need it. It has 19 digits and in this library it is always a string; store it as text in your database too, not asBIGINT(see Two identifiers).- Send the user to
body.info.paymentUrl.web(the web page) or.app(opens LINE Pay on a phone).
5. After the user approves: confirm
After the user approves in LINE Pay, LINE Pay sends them back to your confirmUrl with a transactionId and an orderId:
https://shop.example/line-pay/confirm?transactionId=2026102700000000001&orderId=order-20261027-001Now call confirm to take the money:
export async function confirmPayment(transactionId: string) {
const { body, meta } = await linePay.confirm({
transactionId,
amount: 1000, // the amount of your order, from your database
currency: 'TWD'
})
console.log(body.info.orderId, meta.durationMs)
return body.info
}The amount must not come from the URL
amount must be the amount of the order in your database. Everything in the URL comes from the user's browser, and the user can change it. The transactionId in the URL must also pass through parseConfirmQuery() first, which only allows 1 to 19 digits. The full approach is in Security.
6. Reading the result
Every call returns { body, meta }:
| Field | Description |
|---|---|
body | The LINE Pay response as it was sent (integers beyond 2^53 are strings). |
meta.operation | The name of the operation, for example 'confirm'. |
meta.durationMs | Total time, retries and waiting included. |
meta.attempts | How many times the request was attempted, including attempts that never connected. |
meta.httpStatus | The HTTP status of the last response. |
meta.recovered | Present only when reconcile inferred the result. See Unknown outcomes and reconcile. |
A failure throws a LinePay*Error. Each has a code and an outcome; see Errors and outcomes.
Next
- What happens in between → The payment flow
- What to do when
confirmtimes out → Unknown outcomes and reconcile - A complete flow with an order state machine → Recipes