This guide explains how to use linekit to build LINE applications.
Currently, linekit is a local monorepo. To use it in the provided examples or a new package within this repo:
# In your package.json
{
"dependencies": {
"@linekit/core": "workspace:*",
"@linekit/messaging": "workspace:*",
"@linekit/login": "workspace:*",
"@linekit/express": "workspace:*"
}
}If you publish these packages to npm, usage would be:
npm install @linekit/core @linekit/messaging @linekit/expressThis is the fastest way to get a bot running.
- A LINE Channel (Messaging API)
- Channel Access Token (Long-lived)
- Channel Secret
import express from "express";
import { createLineApp } from "@linekit/core";
import { lineMiddleware } from "@linekit/express";
// 1. Configure
const config = {
channelId: "YOUR_CHANNEL_ID",
channelSecret: "YOUR_CHANNEL_SECRET",
channelAccessToken: "YOUR_ACCESS_TOKEN",
};
// 2. Initialize App
const app = express();
const line = createLineApp(config);
// 3. Define Routes
app.post(
"/webhook",
// Middleware handles signature validation automatically
lineMiddleware(config),
// Router handles event dispatching
line.router({
// Handle Text Messages
message: async (ctx) => {
const { message } = ctx.event;
if (message.type === "text") {
await ctx.replyText(`Echo: ${message.text}`);
}
},
// Handle Follow Events (Friend added)
follow: async (ctx) => {
await ctx.pushText("Thanks for adding me!");
},
// Handle Postbacks (Button clicks)
postback: async (ctx) => {
console.log("Data:", ctx.event.postback.data);
}
})
);
app.listen(3000);Use @linekit/login to handle LINE Login for web apps.
- A LINE Channel (LINE Login)
When your frontend sends an ID Token (from LIFF or iOS/Android SDK):
import { login } from "@linekit/login";
try {
const profile = await login.verify(idToken, "YOUR_CHANNEL_ID");
console.log(`User: ${profile.name}, Email: ${profile.email}`);
} catch (error) {
console.error("Invalid token");
}To manually redirect users to login:
import { generateAuthUrl, issueAccessToken } from "@linekit/login";
// 1. Generate URL
const url = generateAuthUrl({
channelId: "YOUR_CHANNEL_ID",
redirectUri: "http://localhost:3000/callback",
state: "random_string",
scope: "profile openid email"
});
// -> Redirect user to `url`
// 2. Handle Callback
// In your /callback route:
const { code } = req.query;
const tokens = await issueAccessToken(
"YOUR_CHANNEL_ID",
"YOUR_CHANNEL_SECRET",
code,
"http://localhost:3000/callback"
);
console.log("Access Token:", tokens.access_token);
console.log("ID Token:", tokens.id_token);The ctx object in your router provides convenient helpers, but you can also use the @linekit/messaging package directly.
import { richMenu } from "@linekit/messaging";
// Create a menu
const menuId = await richMenu.create(token, {
size: { width: 2500, height: 1686 },
selected: true,
name: "Main Menu",
chatBarText: "Open Menu",
areas: [/* ... defined areas ... */]
});
// Link to a specific user
await richMenu.linkUser(token, userId, menuId);import { multicast } from "@linekit/messaging";
await multicast(token, ["userId1", "userId2"], [
{ type: "text", text: "Broadcast message" }
]);