Using the API

Using the API

Everything you can do in the panel for your own servers, you can also do from a script, a bot or a terminal. This page shows how, with copy-and-paste examples.

The address of this panel is https://tech-code.dev. Every example below uses it.

1. Create an API key

  1. Sign in and open Account → Security → API keys.
  2. Press Create, give the key a name (for example my-script), and choose Read-only if the script only needs to look at things.
  3. Copy the key. It starts with rdk_ and is shown only once.

Good to know:

  • A key acts as you, never more. It can only reach the servers you own or were given access to, and only with the permissions you have there.
  • A read-only key can only read (GET requests). It cannot start servers, change files or open the live console.
  • You can have up to 10 keys. Delete a key any time under the same page; it stops working at once.
  • Keys cannot change your password, two-factor login or other keys. You do that in the browser.
  • Treat a key like a password: don't post it in chat or commit it to a public repository.

2. Your first request

Put the key in a variable, then ask for your servers:

KEY="rdk_paste_your_key_here"
PANEL="https://tech-code.dev"

curl -s -H "Authorization: Bearer $KEY" "$PANEL/api/servers"

You get a JSON list. Each server has an id (like 22e87ac1), a name, a status (running, offline, …), its port, and its limits. Use the id in the examples below.

ID="22e87ac1"

3. Start, stop and restart

curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"action":"restart"}' "$PANEL/api/servers/$ID/power"

action can be start, stop, restart or kill (kill stops it immediately). You need the power permission. The request returns straight away; the server changes state in the background, so check the status again after a few seconds:

curl -s -H "Authorization: Bearer $KEY" "$PANEL/api/servers/$ID" | grep -o '"status":"[a-z]*"' | head -1

4. Send a console command

curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"command":"say Hello from the API"}' "$PANEL/api/servers/$ID/command"

The server must be running, and you need the console permission. The output of the command appears in the console on the panel.

5. Work with files

Paths are relative to the server's top folder. Leave path empty for the top folder. You need the files permission to read and files.write to change things.

List a folder:

curl -s -H "Authorization: Bearer $KEY" "$PANEL/api/servers/$ID/files?path=public"

Read a text file (up to 5 MB):

curl -s -H "Authorization: Bearer $KEY" "$PANEL/api/servers/$ID/files/content?path=public/index.html"

Create or replace a text file (the request body is the new content):

curl -s -X PUT -H "Authorization: Bearer $KEY" \
  --data-binary '<h1>Hello</h1>' "$PANEL/api/servers/$ID/files/content?path=public/index.html"

Upload one or more files into a folder:

curl -s -X POST -H "Authorization: Bearer $KEY" \
  -F "file=@./index.html" "$PANEL/api/servers/$ID/files/upload?path=public"

Download a file:

curl -s -H "Authorization: Bearer $KEY" -o backup.zip "$PANEL/api/servers/$ID/files/download?path=backup.zip"

Create a folder, rename, and delete:

curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"path":"public/images"}' "$PANEL/api/servers/$ID/files/mkdir"

curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"from":"public/old.html","to":"public/new.html"}' "$PANEL/api/servers/$ID/files/rename"

curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"root":"public","paths":["new.html"]}' "$PANEL/api/servers/$ID/files/delete"

Deleting is permanent, so make a backup first if you are not sure (see below).

6. Backups

# list
curl -s -H "Authorization: Bearer $KEY" "$PANEL/api/servers/$ID/backups"

# create one (it runs in the background and shows as "pending" until it is done)
curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"name":"before-update"}' "$PANEL/api/servers/$ID/backups"

Each backup has an id. You can download it with GET /api/servers/$ID/backups/BACKUP_ID/download and restore it with POST /api/servers/$ID/backups/BACKUP_ID/restore. You need the backups permission, and the server must have backups switched on.

7. Change the startup command

curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"startup":"node index.js"}' "$PANEL/api/servers/$ID/startup"

You can also set the value of a variable the server already has: {"env":{"APP_PORT":"3000"}}. You need the startup permission. Restart the server for the change to apply.

8. Errors and what they mean

Errors come back as JSON like {"error":"…"}, with one of these status codes:

| Code | Meaning | |---|---| | 400 | The request was wrong (a missing field, a bad path, a file that is too big). The message says what. | | 401 | The key is missing, wrong, or was deleted. Check the Authorization: Bearer rdk_… header. | | 403 | The key is read-only, or you don't have the permission for that server. | | 404 | There is no such server or file, or you have no access to it. |

9. A small script

This restarts a server and waits until it is running again:

#!/bin/bash
KEY="rdk_paste_your_key_here"; PANEL="https://tech-code.dev"; ID="22e87ac1"
api() { curl -s -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" "$@"; }

api -X POST -d '{"action":"restart"}' "$PANEL/api/servers/$ID/power"
for i in $(seq 1 30); do
  sleep 2
  api "$PANEL/api/servers/$ID" | grep -q '"status":"running"' && { echo "running"; exit 0; }
done
echo "still not running"; exit 1

Full list of endpoints

This page covers the common ones. Every endpoint of the panel (about 300) is listed in the project's API reference. Endpoints marked user (server access) there are the ones your key can use; admin endpoints need an administrator account.