This API should be called on the server side and cannot be called directly on the client side (Mini Program, web page, APP, etc.). For details, please refer to API Call Guide.
API English Name: code2Session
Verifies login credentials. After obtaining the temporary login credential code via the wx.login API, send it to your developer server and call this API to complete the login flow. For more usage details, see Mini Program Login.
1. Call Method
HTTPS Call
GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JS_CODE&grant_type=GRANT_TYPE
Cloud Call
- This API does not support cloud call.
Third-party Call
- This API does not support third-party platform calls.
2. Request Parameters
Query Parameters Query String Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| appid | string | Yes | Mini Program appId |
| secret | string | Yes | Mini Program appSecret |
| js_code | string | Yes | The `code` obtained during login. You can retrieve it via wx.login. |
| grant_type | string | Yes | The authorization type. Enter `authorization_code` here. |
Request Body Request Payload
None
3. Response Parameters
Response Body Response Payload
| Parameter | Type | Description |
|---|---|---|
| session_key | string | Session key |
| unionid | string | The unique identifier of the user on the Open Platform. This is returned if the current Mini Program is bound to a WeChat Open Platform account. For details, see UnionID Mechanism Description. |
| openid | string | Unique user identifier |
| errcode | number | Error code. Returned when the request fails. |
| errmsg | string | Error message. Returned when the request fails. |
4. Notes
No special notes for this API
5. Code Example
Request Example
GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
Response Example
{
"openid": "xxxxxx",
"session_key": "xxxxx",
"unionid": "xxxxx",
"errcode": 0,
"errmsg": "xxxxx"
}
6. Error Codes
The following is the error code list of this API, other error codes can refer to Common Error Codes; When encountering errors in calling the API, you can use the official API Diagnostic Tool to assist in locating and analyzing problems.
| Error Code | Error Description | Solution |
|---|---|---|
| -1 | system error | The system is busy. Please try again later. |
| 40029 | Invalid `code` | Invalid `js_code` |
| 40226 | code blocked | High-risk user. Mini Program login is intercepted. For details on risk levels, see User Security Solutions |
| 45011 | api minute-quota reach limit mustslower retry next minute | API calls are too frequent. Please try again later. |
7. Applicable Scope
| Mini Program | Mini Game |
|---|---|
| ✔ | ✔ |
- ✔:This account type can call this API。
- Other account types not explicitly declared cannot call this API unless otherwise specified.