A production-minded LINE Messaging API webhook starter for Node.js + TypeScript.
This repository is intended to show how an external development partner can receive a LINE webhook, verify it safely, prevent duplicate processing, test the behavior, and hand the implementation over in a form another team can operate.
This is an independent sample maintained by Hakufukusha LLC. It is not an official LY Corporation repository.
- raw-body
x-line-signatureverification using HMAC-SHA256 - verification before JSON parsing or formatting
webhookEventIdbased duplicate detection- handling of webhook redelivery without replying twice
- failure behavior that returns non-2xx so LINE can redeliver when enabled
- structured logs that avoid message text, user IDs, tokens, and raw webhook bodies
- optional reply-message client using the Messaging API
- health/readiness endpoints
- request body size limits
- Node.js built-in test runner + TypeScript strict mode
- GitHub Actions CI
- LIFF browser example with a pinned LIFF SDK version
- production, security, release, and handoff checklists
LINE Platform
|
| HTTPS POST + x-line-signature
v
POST /webhook
|
+--> read exact raw bytes
+--> verify HMAC-SHA256 signature
+--> JSON parse only after verification
+--> inspect webhookEventId
+--> skip already processed event
+--> execute event handler
+--> mark event processed only after success
+--> 200 OK
On handler failure:
event is not marked processed -> 500 -> eligible for LINE webhook redelivery
- Node.js 22 or later
- A LINE Messaging API channel
- Channel secret
- Channel access token only if you enable the reply example
The official LINE Node.js SDK currently requires Node.js 22 or later. This repository deliberately implements signature verification with Node's built-in crypto module so the security-critical calculation is visible and testable.
cp .env.example .env
npm install
npm run buildSet at least:
LINE_CHANNEL_SECRET=your-real-channel-secret
REPLY_MODE=noneThen start:
set -a
source .env
set +a
npm startDefault endpoints:
GET /healthz
GET /readyz
POST /webhook
Configure the Messaging API webhook URL as:
https://your-domain.example/webhook
The default is deliberately REPLY_MODE=none. The starter receives and validates events without sending a message.
To enable a minimal echo reply:
REPLY_MODE=echo
LINE_CHANNEL_ACCESS_TOKEN=your-channel-access-tokenThe handler never logs the received message text.
LINE calculates the x-line-signature from the request body using HMAC-SHA256 and the channel secret. If middleware parses, formats, normalizes newlines, unescapes characters, or otherwise changes the body before verification, a valid request can fail verification.
This repository therefore uses Node's HTTP server directly and verifies the original Buffer before calling JSON.parse().
LINE documents that the same webhook event can arrive more than once and recommends using webhookEventId to detect duplicates. Redelivered events retain the same webhook event ID, while deliveryContext.isRedelivery indicates redelivery.
The included MemoryEventStore is intentionally suitable only for local/demo use. Production systems should replace it with a durable store such as a database unique constraint or Redis persistence.
See docs/WEBHOOK_REDELIVERY.md.
npm run checkThe test suite covers:
- the official LINE signature-verification example
- modified raw body rejection
- missing/invalid signature rejection
- valid webhook acceptance
- LINE webhook verification request with
events: [] - duplicate webhook event suppression
- failed event processing remaining retryable
- malformed JSON after valid signature
- body-size limit
- health endpoint
examples/liff/ contains a minimal browser-side LIFF example pinned to LIFF SDK v2.32.0. It initializes LIFF and displays profile data only in the browser.
It intentionally does not send the object returned by liff.getProfile() to the server. LINE's documentation advises sending an ID token or access token to the server and validating it with LINE when server-side user identity is required.
Related technical article (Japanese): LIFF SDK旧バージョンの利用制限が開始|403エラーの原因と2.32.0への更新検証.
src/
app.ts HTTP routing and webhook boundary
config.ts environment validation
logger.ts structured, low-data logging
server.ts composition root
handlers/
event-handler.ts application event handling
idempotency/
event-store.ts duplicate event abstraction + demo store
line/
reply-client.ts Messaging API reply call
signature.ts raw-body signature verification
types.ts minimal webhook types
tests/ executable tests
docs/ security / production / handoff notes
examples/liff/ minimal LIFF client example
.github/workflows/ci.yml CI
This starter is intentionally small. Before production, at minimum:
- Replace
MemoryEventStorewith durable deduplication. - Move event work to a durable queue if processing may be slow.
- Store channel secrets/tokens in your platform secret manager.
- Define log retention and personal-data handling.
- Add monitoring, alerting, and webhook error-statistics review.
- Define deployment, rollback, access removal, and incident escalation.
- Verify LINE Developers Console settings, including webhook redelivery if used.
Use docs/PRODUCTION_CHECKLIST.md.
A delivery of this component should normally include:
- source repository + release tag/commit
.env.examplewithout secrets- test results
- architecture/data-flow note
- deployment runbook
- rollback runbook
- known issues / exclusions
- access return checklist
- secret rotation or deletion confirmation where applicable
Only official LINE/LY documentation is used for LINE-specific behavior in this repository. See docs/OFFICIAL_REFERENCES.md.
MIT. LINE and LIFF are trademarks/services of their respective owner(s). This repository does not include LINE credentials or customer data.