npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

fastify-line

v0.2.0

Published

Fastify plugin for the LINE Messaging API

Readme

fastify-line

CI NPM version Checked with Biome MIT License

A Fastify plugin for seamless integration with the LINE Messaging API. It provides signature verification, raw body parsing, and exposes the official LINE Messaging API client on your Fastify instance.


Features

  • Signature verification for LINE webhooks
  • Raw body parsing for webhook requests
  • Exposes the official @line/bot-sdk Messaging API client as fastify.line
  • Easy route integration with Fastify

Installation

npm install fastify-line @line/bot-sdk

Note: @line/bot-sdk is a peer dependency and must be installed separately.

Compatibility

| Plugin Version | Fastify Version | |:--------------:|:---------------:| | >=0.x | ^5.x |

Usage

Register the plugin with your LINE channel credentials:

import Fastify from 'fastify'
import fastifyLine, { type WebhookRequestBody } from 'fastify-line'

const fastify = Fastify()

await fastify.register(fastifyLine, {
  channelSecret: process.env.LINE_CHANNEL_SECRET!,
  channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN!,
})

fastify.post<{ Body: WebhookRequestBody }>(
  '/webhook',
  {
    config: { lineWebhook: true }, // Enable LINE webhook handling for this route
  },
  async (request, reply) => {
    const { events } = request.body

    for (const event of events) {
      if (event.type === 'message' && event.message.type === 'text') {
        await fastify.line.client.replyMessage({
          replyToken: event.replyToken,
          messages: [
            {
              type: 'text',
              text: `You said: ${event.message.text}`,
            },
          ],
        })

        // or use blobClient
        // fastify.line.blobClient
      }
    }

    reply.send({ ok: true })
  },
)

await fastify.listen({ port: 3000 })

Skip Signature Verification

await fastify.register(fastifyLine, {
  channelSecret: process.env.LINE_CHANNEL_SECRET!,
  channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN!,
  skipVerify: true, // Skip signature verification
})

Warning: Only use skipVerify: true in development/testing environments. Always verify signatures in production to ensure webhook security. If you skip automatic verification, you can manually verify signatures in your webhook handler using validateSignature from @line/bot-sdk.

Options

| Option | Type | Required | Default | Description | |----------------------|----------|----------|---------|------------------------------------------------------------------| | channelSecret | string | Yes | - | Your LINE channel secret (for signature verification) | | channelAccessToken | string | Yes | - | Your LINE channel access token (for Messaging API client) | | skipVerify | boolean | No | false | Skip signature verification |

How It Works

  • Adds a line property to your Fastify instance: fastify.line (an instance of MessagingApiClient from @line/bot-sdk).
  • For routes with config: { lineWebhook: true }:
    • Parses the raw request body.
    • Verifies the X-Line-Signature header using your channel secret.
    • Throws MissingSignatureError or InvalidSignatureError if verification fails.

Error Handling

The plugin throws custom errors for signature issues:

  • MissingSignatureError: Thrown if the X-Line-Signature header is missing.
  • InvalidSignatureError: Thrown if the signature is invalid.

You can handle these errors using Fastify's error handler:

fastify.setErrorHandler((err, _request, reply) => {
  if (err instanceof MissingSignatureError) {
    reply.status(401).send({
      error: err.message,
      message: 'The X-Line-Signature header is missing.',
    })
  }

  if (err instanceof InvalidSignatureError) {
    reply.status(401).send({
      error: err.message,
      message: 'The X-Line-Signature header is invalid.',
      signature: err.signature,
    })
  }

  // Default error handling
  reply.send(err)
})

Types

This plugin augments Fastify's types:

  • fastify.line: The LINE Messaging API client
  • config.lineWebhook: Set to true on a route to enable LINE webhook handling

Contributing

Contributions are welcome!

License

MIT