Skip to main content

Public API Usage

SnailyCADv4 allows you to create scripts that can communicate with to it. You'll find out how below.

Creating the token​

Firstly, we must create the token. Head over to the cad-settings page and open the Api Token tab. Once there, it will allow you to enable Public API Access and generate a token. Once you've generated a token you can copy it.

Make sure to not share this token with anyone. If this token gets leaked make sure to re-generate it ASAP.

Using the token​

When making a script that communicates to the CAD, you can add the token in the headers of the request.

info

Since version 1.0.0-beta.54, there is Swagger UI documentation that can be found at: http://MY_CAD_API_URL/api-docs

JavaScript Example

const fetch = require("node-fetch");

// get the token from `server.cfg`
const token = GetConvar("snailycad_v4_api_token");
// example.com/v1
const apiUrl = GetConvar("snailycad_v4_api_url");

// this must be: "snaily-cad-api-token"
const API_TOKEN_HEADER_NAME = "snaily-cad-api-token";

const USER_API_TOKEN_HEADER_NAME = "snaily-cad-user-api-token";

// send a request with the global CAD's API Token
async function getValuesFromCAD() {
// get all the vehicles, weapons, genders and ethnicities
const response = await fetch(`${apiUrl}/admin/values/vehicle?paths=weapon,gender,ethnicity`, {
headers: {
// send the token via headers.
[API_TOKEN_HEADER_NAME]: token,
},
});
const json = response.ok && (await response.json());

if (json) {
console.log("data from API: ", { json });
}
}

// send a request with the user's API token.
// Note: only available from version 1.0.0-beta.89
async function getMyCitizens() {
// get all the citizens of the authenticated user
const response = await fetch(`${apiUrl}/citizen`, {
headers: {
// send the token via headers.
[USER_API_TOKEN_HEADER_NAME]: "user api token",
},
});
const json = response.ok && (await response.json());

if (json) {
console.log("data from API: ", { json });
}
}

LUA Example

local API_URL = "URLHERE"
local API_TOKEN = "TOKENHERE"
local API_TOKEN_HEADER_NAME = "snaily-cad-api-token"

PerformHttpRequest(
API_URL .. "admin/values/vehicle?paths=weapon,gender,ethnicity",
function(errorCode, resultData, resultHeaders)
print("Returned error code:" .. tostring(errorCode))
print("Returned data:" .. tostring(resultData))
print("Returned result Headers:" .. tostring(resultHeaders))
end,
"GET",
json.encode("{}"),
{
[API_TOKEN_HEADER_NAME] = API_TOKEN,
["Content-Type"] = "application/json"
}
)

Was this page helpful?