Prerequisites
- Register for both a WeChat Official Account and a Mini Program. Official documentation covers the registration steps in detail.
- Bind the Official Account to the Mini Program. This binding is required for message push functionality.
Mini Program Development Process
- Register the Mini Program at https://mp.weixin.qq.com/cgi-bin/wx.
- To enable WeChat message push, register and log in to the WeChat Official Account development platform, then add the Mini Program (requires a 300 RMB payment). URL: https://mp.weixin.qq.com/cgi-bin/home.
- Obtain the Mini Program's
appidandsecretfrom the Mini Program backend management page. - All interactions with WeChat require HTTPS. Use a domain name with a proper SSL certificate (e.g., configure it under Nginx). The Mini Program backend must expose a public port.
- For message push, the Mini Program must be bound to an Official Account. Also add the server IP to the IP whitelist; otherwise, you cannot obtain an access token.
- For image upload, use a dedicated image server and return the image URL to the frontend. When using Nginx, adjust the
client_max_body_sizedirective (e.g.,20M) to allow larger uploads. - The Mini Program package size must not exceed 2 MB. Store static resources such as images on an external static server.
- To test the Mini Program in experience version, add developers in the WeChat management backend. When logging in, enable development debugging (via the three-dot menu in the top-right corner). The experience version requires the backend service to be on the same local network for access.
- For Mini Program release, submit an audit for review (first review may take up to 3 days or more; subsequent reviews usually take about 1 day). Before going live, add the IP whitelist for token retrieval on the WeChat Official Account platform and ensure the backend service has public network access.
Official Account Development Process
This section focuses on message push using the Official Account.
WeChat API Documentation
- Official Account interface permissions (admin required):
https://mp.weixin.qq.com/advanced/advanced?action=table&token=396006949&lang=zh_CN - Official Account development documentation:
https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
Message Push Workflow
WeChat template message interface: https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
Prerequisites for Message Push
To associate users between the Mini Program and the Official Account, you need the unionid. This requires binding both the Mini Program and Official Account to the same WeChat Open Platform. Key concepts:
- openid: Unique identifier for a user within a specific Mini Program or Official Account. Different for each application. Similar to a UUID.
- unionid: Common identifier across Mini Programs and Official Accounts under the same WeChat Open Platform. Obtain via
https://api.weixin.qq.com/sns/jscode2session, but the user must have used both the Mini Program and followed the Official Account (requires binding: Official Account, Mini Program, Open Platform). - appid: Unique identifier for the Mini Program or Official Account itself.
- secret: Developer password for verifying identity. Keep it secure; do not store in code or share with third parties.
Step-by-Step Push Process
-
User follows Official Account: Obtain the user's openid and unionid for the Official Account.
- Get the Offficial Account access token:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET - Get the list of followers (openids). First request does not require
next_openid:https://api.weixin.qq.com/cgi-bin/user/get?access_token=TOKEN&next_openid=APPID - Get unionid for a user (single or batch):
- Single:
GET https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN - Batch:
POST https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN
- Single:
- Get the Offficial Account access token:
-
User uses Mini Program: Obtain the user's openid and unionid for the Mini Program.
- Get the Mini Program access token (same endpoint as Official Account, but with different appid/secret):
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET - Get session_key, openid, and unionid. The
codecomes from the frontend after login:https://api.weixin.qq.com/sns/jscode2session?appid=APPID&js_code=CODE&grant_type=authorization_code
- Get the Mini Program access token (same endpoint as Official Account, but with different appid/secret):
-
Associate using unionid and send template message:
- Use the unionid to link the user across platforms, then send a template message. Construct a JSON payload containing the user's openid, template ID (available from the Official Account template management page), and template data.
- Endpoint:
POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
Flow Diagram
(A simple diagram of the process can be included here.)