Send an SMS Message
Send an SMS text message from one of your 2Chat numbers to any phone number.
Endpoint
POST https://api.p.2chat.io/open/sms/send
Authentication
Include your API key in the X-User-API-Key header. Learn more about authentication.
Parameters
These parameters should be sent as JSON payload in the request body.
| Field | Type | Required | Description |
|---|---|---|---|
from_number | string | Yes | Your 2Chat SMS number in E.164 format (e.g. +17137157533) |
to_number | string | Yes | The recipient's phone number in E.164 format (e.g. +442079460958) |
text | string | Yes | The message content (UTF-8, up to 160 characters per SMS segment) |
Important notes
- Phone numbers must be in E.164 international format (e.g.
+1XXXXXXXXXX) - Messages longer than 160 characters will be split into multiple SMS segments and billed accordingly
- The
from_numbermust be an SMS-enabled number connected to your 2Chat account - A2P messaging to US numbers is not currently supported
Invocation
- cURL
- Python
- JavaScript
- PHP
curl --location --request POST 'https://api.p.2chat.io/open/sms/send' \
--header 'Content-Type: application/json' \
--header 'X-User-API-Key: your_api_key_here' \
--data '{
"from_number": "+17137157533",
"to_number": "+442079460958",
"text": "Hello from 2Chat!"
}'
import requests
url = "https://api.p.2chat.io/open/sms/send"
headers = {
"Content-Type": "application/json",
"X-User-API-Key": "your_api_key_here"
}
payload = {
"from_number": "+17137157533",
"to_number": "+442079460958",
"text": "Hello from 2Chat!"
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
const axios = require('axios');
const response = await axios.post('https://api.p.2chat.io/open/sms/send', {
from_number: '+17137157533',
to_number: '+442079460958',
text: 'Hello from 2Chat!'
}, {
headers: {
'Content-Type': 'application/json',
'X-User-API-Key': 'your_api_key_here'
}
});
console.log(response.data);
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.p.2chat.io/open/sms/send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode([
'from_number' => '+17137157533',
'to_number' => '+442079460958',
'text' => 'Hello from 2Chat!'
]),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-User-API-Key: your_api_key_here'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Response
The API will return whether it succeeded or not to queue the message for sending.
Success (HTTP 202 Accepted)
{
"success": true,
"message_id": "abc123-def456-ghi789"
}
| Field | Description |
|---|---|
success | true when the request was accepted and successful |
message_id | The internal ID of the message you can use to keep track of it |
Error
{
"error": true,
"error_message": "Description of what went wrong"
}
| Field | Description |
|---|---|
error | true when the request failed |
error_message | A description of what went wrong |