Create a WhatsApp connection
With this endpoint, you will be able to create a new QR-code based WhatsApp connection in your account.
Parameters
These parameters should be sent as a JSON payload in the request body.
Field | Description | Example values |
---|---|---|
phone_number | The number you want to connect to 2Chat | +18647351567 |
friendly_name | A name or alias for the number | My business number |
Invocation
- cURL
- Python
- NodeJS
curl -X POST --location 'https://api.p.2chat.io/open/whatsapp/channel/create' \
--header 'Content-Type: application/json' \
--header 'X-User-API-Key: your_api_key_here' \
--data '{
"phone_number": "+18647351567",
"friendly_name": "Testing number creation"
}'
import requests
import json
url = "https://api.p.2chat.io/open/whatsapp/channel/create"
payload = json.dumps({
"phone_number": "+18647351567",
"friendly_name": "Testing number creation"
})
headers = {
'Content-Type': 'application/json',
'X-User-API-Key': 'your_api_key_here'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
const axios = require('axios');
let data = JSON.stringify({
"phone_number": "+18647351567",
"friendly_name": "Testing number creation"
});
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://api.p.2chat.io/open/whatsapp/channel/create',
headers: {
'Content-Type': 'application/json',
'X-User-API-Key': 'your_api_key_here'
},
data : data
};
axios.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
Response
If the number didn't exist and the creation was successful, you will see the following output and immediately after the number will try to connect and generate a QR code that you will need to scan.
To get that QR code information you need to call the Channel Status Endpoint using the UUID
value (WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97
) you obtained in this step.
You can also get the UUID of a number you want to manipulate with the List Numbers Endpoint.
{
"success": true,
"channel": {
"id": "WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97",
"uuid": "WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97",
"friendly_name": "Testing number creation now",
"phone_number": "+18647351566",
"iso_country_code": "US",
"pushname": null,
"server": null,
"platform": null,
"connection_status": "D",
"enabled": true,
"is_business_profile": false,
"channel_type": "WW",
"sync_contacts": false,
"timezone": "America/New_York",
"lang": "en",
"created_at": "2023-10-12T17:43:58Z",
"updated_at": null
}
}
Getting the QR Code to connect the number
Once the channel is created, the connect
command will be executed immediately getting the number ready for establishing a QR code connection.
You have 2 options to get the QR code:
- Subscribe to the Channel Status Webhook and get the QR code value in real-time.
- Explicitly obtain the latest QR code received using the following endpoint:
Invocation
Using the UUID (WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97
) we obtained after creating the connection, we will use it to get the QR code value.
You can poll for new QR code values every 5 seconds as that's the minimum time it can take for it to be updated.
- cURL
- Python
- NodeJS
curl -X GET --location 'https://api.p.2chat.io/open/whatsapp/channel/WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97/qr-code' \
--header 'Content-Type: application/json' \
--header 'X-User-API-Key: your_api_key_here'
import requests
import json
url = "https://api.p.2chat.io/open/whatsapp/channel/WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97/qr-code"
headers = {
'Content-Type': 'application/json',
'X-User-API-Key': 'your_api_key_here'
}
response = requests.request("GET", url, headers=headers)
print(response.text)
const axios = require('axios');
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://api.p.2chat.io/open/whatsapp/channel/WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97/qr-code',
headers: {
'Content-Type': 'application/json',
'X-User-API-Key': 'your_api_key_here'
}
};
axios.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
Response
Field | Description |
---|---|
qr_code | The QR code value, set to null when no connect command was executed |
qr_code_image_url | The URL of the QR code image |
warning | A message indicating why the QR code is not present when that's the case |
channel | The channel details |
{
"success": true,
"qr_code": "2@X2gWRMVmfb5UuQPonVzwgKrULbaH9tXWUZ4T22EQVMJLozt4p4+tVWn579wftoOoHhVjXRUEl5Qovycbhvhg/nmb/nBeKOBdCWI=,HLoV1HJt1holPlRG+uP444TTXysWR7tymHmVafklz24=,u3A10kNHhj2d9xZDw06ANBbWfHHZ5MTqRjHgFDz4IFg=,IEZNp3uawofMm9Mwn+Tc99vgSH3Ck7GlXQAKEhvvRyA=,1",
"qr_code_image_url": "https://.../ACC91be87af-5a29-4034-b599-342f2aeb5d52/tmp/WPNf8a36be9-508e-4715-a1ce-9a2352baef1d/qrcode-429de06f7f9d25591f765fe5ab32aa3c.png",
"channel": {
"id": "WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97",
"uuid": "WPNf3ea6c85-0dca-4d8a-a9a6-2b918f59cc97",
"friendly_name": "Testing number creation now",
"phone_number": "+18647351566",
"iso_country_code": "US",
"pushname": null,
"server": null,
"platform": null,
"connection_status": "D",
"enabled": true,
"is_business_profile": false,
"channel_type": "WW",
"sync_contacts": false,
"timezone": "America/New_York",
"lang": "en",
"created_at": "2023-10-12T17:43:58Z",
"updated_at": null
}
}