Skip to content

Instantly share code, notes, and snippets.

@kingdudely
Created May 2, 2026 02:52
Show Gist options
  • Select an option

  • Save kingdudely/6239dcfaf5ae32268b58cd246ed9517e to your computer and use it in GitHub Desktop.

Select an option

Save kingdudely/6239dcfaf5ae32268b58cd246ed9517e to your computer and use it in GitHub Desktop.
Simple Function That Handles Using Roblox Api For You
async function robloxApiRequest({
robloSecurity,
apiKey,
method,
subdomain,
path,
body,
}) {
if (body != null && typeof(body) !== "string") {
body = JSON.stringify(body);
};
const url = `https://${subdomain}.roblox.com/${path}`;
const headers = new Headers({
"X-API-Key": apiKey,
"Content-Type": "application/json",
"User-Agent": "Roblox/WinInet",
"Origin": "https://www.roblox.com",
"Referer": "https://www.roblox.com/",
"Cookie": robloSecurity ? `.ROBLOSECURITY=${robloSecurity};` : undefined,
});
const options = {
"headers": headers,
"body": body,
"method": method,
};
let response = await fetch(url, options);
if (response.status === 403) {
const csrfToken = response.headers.get("x-csrf-token");
if (csrfToken) {
headers.set("x-csrf-token", csrfToken);
response = await fetch(url, options);
}
}
const contentType = response.headers.get("content-type") || "";
let result;
if (contentType.includes("application/json")) {
result = await response.json();
const errors = result?.errors;
if (!response.ok && Array.isArray(errors)) {
throw new Error(errors.map(error => error.message || String(error)).join("\n"));
}
} else {
result = await response.text();
}
if (!response.ok) {
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment