DiceXDiceX/ Blog

How to send SMS via API: your first DiceX request in cURL, JavaScript, Python and PHP

A short quick-start: create a key, make one POST request to SendSingleSMS with the key in the ApiKey header, read the messageId from the response and check the status.

4 min read

To send SMS via API with DiceX, you make one HTTPS POST request to https://gateway.dicex.me/api/v1/Sms/SendSingleSMS, with your API key in the ApiKey header and a JSON body holding three fields: the sending line (lineNumber), the recipient (receptor) and the text (message). The response gives you a messageId you can use to check the message's status. Below are the steps and the same request in cURL, JavaScript, Python and PHP, the same samples DiceX publishes for developers, built on the endpoint and field names of the public OpenAPI description.

Before you start

You need three things:

  1. A DiceX account. The same account works for every DiceX service.
  2. An API key. Create it in the developer panel. Give it a clear title and, if you like, a spending cap. New keys are reviewed by the DiceX team, and you can use the key once it is approved.
  3. Credit in your wallet. Each SMS is paid from your wallet, as in the panels. Then put the key in an environment variable, so it never ends up in your code or your repository:
    ```bash
    export DICEX_API_KEY="your-key-here"
    ```

The request at a glance

Part

Value

Method

POST

URL

https://gateway.dicex.me/api/v1/Sms/SendSingleSMS

Header

ApiKey: <your key>

Header

Content-Type: application/json

Body

lineNumber, receptor, message


The body fields:

  • lineNumber: the sending line the message goes out from.
  • receptor: the recipient's mobile number, in international format.
  • message: the text of the SMS.

    The request also accepts optional fields: sendDate (a date-time), clientReferenceId (your own reference, returned in the response) and udh.

Send SMS via API: the code


The samples use the same placeholder line, number and text. Replace them with your own sending line and a number you can check, such as your own phone.

cURL


```bash
curl -X POST https://gateway.dicex.me/api/v1/Sms/SendSingleSMS \
-H "ApiKey: $DICEX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"lineNumber": "30004567",
"receptor": "+96890000000",
"message": "Your login code is 482190"
}'
```

JavaScript


```javascript
const response = await fetch("https://gateway.dicex.me/api/v1/Sms/SendSingleSMS", {
method: "POST",
headers: {
ApiKey: process.env.DICEX_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
lineNumber: "30004567",
receptor: "+96890000000",
message: "Your login code is 482190",
}),
});
```

Python


```python
import os
import requests

response = requests.post(
"https://gateway.dicex.me/api/v1/Sms/SendSingleSMS",
headers={"ApiKey": os.environ["DICEX_API_KEY"]},
json={
"lineNumber": "30004567",
"receptor": "+96890000000",
"message": "Your login code is 482190",
},
)
```

PHP


```php
$ch = curl_init("https://gateway.dicex.me/api/v1/Sms/SendSingleSMS");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"ApiKey: " . getenv("DICEX_API_KEY"),
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => json_encode([
"lineNumber" => "30004567",
"receptor" => "+96890000000",
"message" => "Your login code is 482190",
]),
]);
$response = curl_exec($ch);
```

Reading the response


A successful call returns JSON with three top-level fields:

  • isSuccess: whether the request succeeded.
  • message: a short message from the API.
  • value: the result, with messageId, lineNumber, receptor, cost, clientReferenceId, message and sendDate.

    Keep the messageId. It is how you refer to this SMS later. If you sent a clientReferenceId, store it next to the messageId so you can match the message to the order, user or event in your own system.

Checking the status


To see what happened to the message, send one or more messageId values to the status endpoint. Its body is a JSON array of IDs:

```bash
curl -X POST https://gateway.dicex.me/api/v1/Sms/CheckSmsStatus \
-H "ApiKey: $DICEX_API_KEY" \
-H "Content-Type: application/json" \
-d '[123456789]'
```

Replace 123456789 with the messageId from your response. The API reference shows the response fields for this and every other endpoint.

Where to go from one SMS

  • Many recipients, one text: POST /api/v1/Sms/SendBulkSMS takes receptors, a list of numbers.
  • Different texts to different recipients: POST /api/v1/Sms/SendPairToPairSMS.
  • Login and verification codes: POST /api/v1/Otp/Send sends a one-time code from a template.
  • Incoming SMS: GET /api/v1/Sms/GetReceivedSmses, or the paging version for larger lists.
  • WhatsApp instead of SMS: POST /api/v1/WhatsApp/SendMessage; see the social messaging hub.

    There is also a GET version of SendSingleSMS that takes the key as a query parameter. Prefer the POST version with the ApiKey header: keys in URLs tend to end up in logs and browser history.

Before you go to production

  • Store the IDs. Save each messageId, with your own clientReferenceId, next to the order or user it belongs to.
  • Handle failures. Check isSuccess on every response and log the message when it is false, instead of assuming the SMS went out.
  • Use separate keys. Give production and testing their own keys, each with its own spending cap and allowed IPs.
  • Check status later, not instantly. Delivery takes a moment; query CheckSmsStatus after a short delay or in a scheduled job.
  • Read the reference. The API reference lists every field and response for the endpoints you add next.

Common mistakes

  • Missing header. The header name is ApiKey. Without it, the request is not authenticated.
  • Key not approved yet. A newly created key works only after the DiceX team approves it.
  • No credit. Top up your wallet before testing.
  • Key in front-end code. Call the API from your server. A key in a mobile app or web page can be copied by anyone.

    Related reading: the overview of the DiceX communications API and the guide to API key security.

Next step


Create your key in the developer panel, run the cURL sample with your own number, then move the call into your app. For an overview of every service the API covers, see the developers hub; for the SMS product itself, the SMS hub.

Frequently asked questions

Which endpoint sends a single SMS?

POST https://gateway.dicex.me/api/v1/Sms/SendSingleSMS, with a JSON body holding lineNumber, receptor and message.

Where does the API key go?

In the ApiKey request header. The samples read it from an environment variable so it is never written into the code.

How do I know whether the SMS was delivered?

The response returns a messageId. Send it to POST /api/v1/Sms/CheckSmsStatus to get the status of that message.

Can I send the same SMS to many numbers?

Yes. Use SendBulkSMS, which takes a list of receptors, or SendPairToPairSMS for different messages to different recipients.

For developers

Related articles