Code examples in our API docs: curl, PHP, Python and JavaScript, ready to copy and adapt

PHP & Development·· 10 views· 3 min read

The short answer: every operation has the same request written in four languages: curl, PHP, Python and JavaScript. All of them read the key from the RLX_API_KEY environment variable, so you can copy them without pasting your key into the code.

The four languages

Language What it uses Where it runs
curl The command line. In a Linux or macOS terminal. Lines end with \, as in a terminal. In Windows PowerShell, type curl.exe on a single line and replace $RLX_API_KEY with $env:RLX_API_KEY.
PHP PHP's cURL extension. On your server or your hosting.
Python The requests library. Anywhere you have Python; install it with pip install requests.
JavaScript fetch, with no libraries. On Node.js 18 or newer, on the server.

The key never goes in the code

Set the variable once, in the environment where the program runs, and every example works:

# Linux and macOS
export RLX_API_KEY="sandbox_..."

# Windows PowerShell
$env:RLX_API_KEY="sandbox_..."

PHP reads it with getenv('RLX_API_KEY'), Python with os.environ["RLX_API_KEY"] and JavaScript with process.env.RLX_API_KEY.

Pick your language once

The examples sit in tabs. When you pick one, every block on the page switches to that language, and your choice is remembered in your browser for the next pages. Each block has a copy button.

What to adapt before using them

1 The example values: domains, service numbers and texts are samples; replace them with your own.
2 The Idempotency-Key on POST requests: the example has a fixed value. In your program, generate a unique identifier for each operation, and reuse it only when you repeat that same operation.
3 The Accept-Language header: it follows the language of the page; change it if you want messages in the other language.
4 Error handling: the examples only print the status and the body. Your program should read the error code and decide what to do.
Do not put the key in the JavaScript of a page on your website. Browsers cannot call the API outside the documentation, and anyone opening the page would see the key. The JavaScript example is meant to run on the server, with Node.js.
Node complains about await? The example uses await outside a function: save the file with the .mjs extension, or wrap the code in an async function. For PHP, there is more detail in calling an API from PHP with cURL.

The first examples, ready to copy, are in Get started with the API.

See the first examples

SEE ALSO

Reading one API operation in our docs: permission, parameters, examples, responses and errors

The API Playground: trying a real request from your browser, step by step

Generating an API client for your language from our OpenAPI specification

Calling a web API from PHP with cURL and reading the JSON reply

A reading path through our API docs: from the first request to a finished integration

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.60/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.