Kodexempel för SMS API

Integrera vårt kraftfulla SMS API på din webbplats eller applikation, så är du redo att börja skicka SMS på några minuter.

Använd våra officiella API-omslag och klientbibliotek för att komma igång snabbt. De är tillgängliga med populära språk som Python, PHP, Node.js, Java och andra.

Det finns inget klientbibliotek för det språk du föredrar? Du kan använda vilket allmänt HTTP-bibliotek du vill.

import requests

url = "https://api.sms.cx/sms"

payload = {
    "to": ["+31612469333"],
    "from": "InfoText",
    "text": "Your confirmation code is 5443"
}
headers = {
    "content-type": "application/json",
    "Authorization": "Bearer <ACCESS_TOKEN>"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.text)
require 'uri'
require 'net/http'
require 'openssl'

url = URI("https://api.sms.cx/sms")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/json'
request["Authorization"] = 'Bearer <ACCESS_TOKEN>'
request.body = "{\"to\":[\"+31612469333\"],\"from\":\"InfoText\",\"text\":\"Your confirmation code is 5443\"}"

response = http.request(request)
puts response.read_body

const data = JSON.stringify({
  "to": [
    "+31612469333"
  ],
  "from": "InfoText",
  "text": "Your confirmation code is 5443"
});

const xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://api.sms.cx/sms");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer <ACCESS_TOKEN>");

xhr.send(data);
const http = require("https");

const options = {
  "method": "POST",
  "hostname": "api.sms.cx",
  "port": null,
  "path": "/sms",
  "headers": {
    "content-type": "application/json",
    "Authorization": "Bearer <ACCESS_TOKEN>"
  }
};

const req = http.request(options, function (res) {
  const chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    const body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({to: ['+31612469333'], from: 'InfoText', text: 'Your confirmation code is 5443'}));
req.end();
<?php

$curl = curl_init();

$payload = [
	'to' => '+31612469333',
	'from' => 'InfoText',
	'text' => 'Your confirmation code is 5443',
];

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.sms.cx/sms",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode($payload),
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer <ACCESS_TOKEN>",
    "content-type: application/json"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"to\":[\"+31612469333\"],\"from\":\"InfoText\",\"text\":\"Your confirmation code is 5443\"}");
Request request = new Request.Builder()
  .url("https://api.sms.cx/sms")
  .post(body)
  .addHeader("content-type", "application/json")
  .addHeader("Authorization", "Bearer <ACCESS_TOKEN>")
  .build();

Response response = client.newCall(request).execute();

var client = new RestClient("https://api.sms.cx/sms");
var request = new RestRequest(Method.POST);

request.AddHeader("content-type", "application/json");
request.AddHeader("Authorization", "Bearer <ACCESS_TOKEN>");
request.AddParameter("application/json", "{\"to\":[\"+31612469333\"],\"from\":\"InfoText\",\"text\":\"Your confirmation code is 5443\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://api.sms.cx/sms"

	payload := strings.NewReader("{\"to\":[\"+31612469333\"],\"from\":\"InfoText\",\"text\":\"Your confirmation code is 5443\"}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("content-type", "application/json")
	req.Header.Add("Authorization", "Bearer <ACCESS_TOKEN>")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
curl --request POST \
  --url https://api.sms.cx/sms \
  --header 'Authorization: Bearer <ACCESS_TOKEN>' \
  --header 'content-type: application/json' \
  --data '{"to":["+31612469333"],"from":"InfoText","text":"Your confirmation code is 5443"}'