Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including koa-wechat with all npm packages installed. Try it out:

var koaWechat = require("koa-wechat")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

koa-wechat v0.1.3

wechat offical account API middleware for koajs


Wechat Official Account API (微信公众平台API) middleware for koajs


var app = require('koa')()
var wechat = require('koa-wechat')

app.use(wechat({ token: 'wechat_token' }))

To use this with weixin-robot:

// Get the robot
app.use(function *(next) {
  var media_id = this.path.split('/')[1]
  if (!media_id) {
  var webot = yield Webot.get(media_id)
  if (!webot) {
  this.webot = webot
  this.wx_token = webot.wx_token
  yield next

app.use(session({ store: redisc('webot:session:') }))

// do the reply
app.use(function *(next) {
  var info = this.req.body
  info.session = this.session
  this.body = yield this.webot.reply(info)
  yield next

// an empty handler to prevent any following middlewares


Token assigned to wechat API.


Find the token from a ctx property, i.e., this[tokenProp]


Whether we need to check signature or not, default to true.


By default, koa-wechat will set a this.sessionId to identify (a offical account + a subscriber) as an unique session. Then you can safely use koa-session middleware to save an subscriber's session. You can set options.session to false to disable this behavior.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free