Skip to content

About

Node.js・TypeScriptによるLINE Messaging API Webhookの開発サンプル。署名検証、イベント重複検出、再送時の処理、LIFF SDK 2.32.0の例と開発引き継ぎ資料を含みます。

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

line-webhook-node-starter

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.

What this demonstrates

  • raw-body x-line-signature verification using HMAC-SHA256
  • verification before JSON parsing or formatting
  • webhookEventId based 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

Completed flow

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

Requirements

  • 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.

Quick start

cp .env.example .env
npm install
npm run build

Set at least:

LINE_CHANNEL_SECRET=your-real-channel-secret
REPLY_MODE=none

Then start:

set -a
source .env
set +a
npm start

Default endpoints:

GET  /healthz
GET  /readyz
POST /webhook

Configure the Messaging API webhook URL as:

https://your-domain.example/webhook

Reply example

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-token

The handler never logs the received message text.

Why the raw body matters

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().

See docs/SECURITY_MODEL.md.

Duplicate events and redelivery

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.

Tests

npm run check

The 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

LIFF example

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.

See docs/LIFF_SECURITY.md.

Related technical article (Japanese): LIFF SDK旧バージョンの利用制限が開始|403エラーの原因と2.32.0への更新検証.

Repository structure

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

Before production

This starter is intentionally small. Before production, at minimum:

  1. Replace MemoryEventStore with durable deduplication.
  2. Move event work to a durable queue if processing may be slow.
  3. Store channel secrets/tokens in your platform secret manager.
  4. Define log retention and personal-data handling.
  5. Add monitoring, alerting, and webhook error-statistics review.
  6. Define deployment, rollback, access removal, and incident escalation.
  7. Verify LINE Developers Console settings, including webhook redelivery if used.

Use docs/PRODUCTION_CHECKLIST.md.

Handoff artifacts

A delivery of this component should normally include:

  • source repository + release tag/commit
  • .env.example without secrets
  • test results
  • architecture/data-flow note
  • deployment runbook
  • rollback runbook
  • known issues / exclusions
  • access return checklist
  • secret rotation or deletion confirmation where applicable

See docs/DELIVERY_HANDOFF.md.

Official references

Only official LINE/LY documentation is used for LINE-specific behavior in this repository. See docs/OFFICIAL_REFERENCES.md.

License

MIT. LINE and LIFF are trademarks/services of their respective owner(s). This repository does not include LINE credentials or customer data.

About

Node.js・TypeScriptによるLINE Messaging API Webhookの開発サンプル。署名検証、イベント重複検出、再送時の処理、LIFF SDK 2.32.0の例と開発引き継ぎ資料を含みます。

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages