Run JavaScript
The Run JavaScript block runs a short piece of JavaScript and passes what it returns to later blocks. Use it for logic that would take many blocks, like going through a list, reading numbers out of a message or several calculations that build on each other. For a single calculation, the Calculate Value block is enough.
The code runs as the body of an async function, so it can use await. End it with return and the value, and later blocks read it with {{result('js_abc')}}, where js_abc is the ID of the block. It can return a number, text, a list or an object, like {{result('js_abc').total}}.
const rolls = [];
for (let i = 0; i < input.dice; i++) {
rolls.push(Math.floor(Math.random() * 6) + 1);
}
return { rolls: rolls.join(", "), total: rolls.reduce((a, b) => a + b, 0) };
Inputs
Values from the flow go in through inputs. Each input has a name and a value, and the code reads it as input.name. An input named dice with the value {{arg('dice')}} is input.dice in the code above.
A value that is a single placeholder keeps its type, so {{arg('dice')}} stays a number and a list stays a list. Users, members, channels, roles and messages become objects with the same fields as their placeholders. An input user with the value {{user}} gives the code input.user.display_name, input.user.mention and input.user.joined_at.
A few fields are left out, because Kite only works them out when a placeholder uses them. Those are role_names, role_mentions, permissions, is_admin, is_owner, top_role and color of a user, the category of a channel, and the counts and channels of a server. Pass them as inputs of their own, like {{user.is_admin}}. {{guild}} and {{channel}} are the ID of the server and channel the flow runs in, so pass the fields you need, like {{guild.name}}.
The response of a Send API Request block has status_code, body with the text of the response and data with the response read as JSON.
Placeholders in the code itself aren't filled in. Otherwise anyone who can put text in them, like a member writing a message, could change the code.
What the code can use
The code can use the JavaScript language up to ES2023 and a few newer additions:
Object,Array,String,Number,Math,Date,JSON,RegExp,SymbolandBigIntMap,Set,WeakMap,WeakSetandWeakRef- Typed arrays like
Uint8Array,ArrayBufferandDataView Proxy,Reflect, generators and iterator helpers- Newer methods like
toSorted,findLast,Object.groupByandSetmethods likeunion parseInt,parseFloat,encodeURIComponent,decodeURIComponent,atobandbtoaperformance.now()
Regular expressions support named groups, lookbehind and Unicode properties like \p{L}, but not the v flag. A pattern that backtracks for too long stops at the time limit.
console.log, console.info, console.warn and console.error write to the app's logs, up to 50 lines and 4 KB per run.
The code can read from and change things in Discord and the app's integrations, use stored variables and send requests to other websites, see below. There's no setTimeout or setInterval, and no require, import, process or Buffer. URL, URLSearchParams, TextEncoder, TextDecoder, crypto and structuredClone aren't available either.
await, async functions and queueMicrotask work. There are no timers, so the code can't wait for time to pass. If it waits for a promise that never finishes, the block fails. An error in a queueMicrotask callback is logged and doesn't stop the code.
There's no Intl, so toLocaleString on dates ignores the language and time zone, and dates are in UTC. Numbers get commas, like 1,234.5. To show a date in a time zone, use formatDate(date, format, timeZone). The date can be a Date, a timestamp in milliseconds or text like 2026-01-01T12:00:00Z.
return formatDate(Date.now(), "dddd [at] HH:mm", "Europe/Berlin");
// Sunday at 14:30
| Token | Example |
|---|---|
YYYY, YY | 2026, 26 |
MMMM, MMM, MM, M | January, Jan, 01, 1 |
DD, D | 05, 5 |
dddd, ddd | Monday, Mon |
HH, H | 14, 14 |
hh, h, A, a | 02, 2, PM, pm |
mm, m, ss, s | 05, 5, 09, 9 |
Z | +01:00 |
Text in [brackets] stays as it is. The time zone is a name like America/New_York, and defaults to UTC. To show a time to each member in their own time zone, use Discord's timestamps instead, like <t:{{user.created_at}}:F>.
Discord and integrations
The code can call Discord, ER:LC and Cookie API with the endpoints of their APIs. Each one is a function that returns a promise, so use await:
const [channel, messages] = await Promise.all([
discord.getChannel({ channel_id: input.channel }),
discord.listMessages({ channel_id: input.channel, limit: 50 }),
]);
return `${channel.name} has ${messages.length} recent messages`;
For Discord, the names are the endpoints of the Discord API Request block in camel case, so get_guild_member is discord.getGuildMember. ER:LC has erlc.getServer, with the parameters Players, Staff, JoinLogs, Queue, KillLogs, CommandLogs, ModCalls, EmergencyCalls and Vehicles written like that, and erlc.runCommand. Cookie API has cookieApi.getCaptcha with captcha_id, cookieApi.getMinecraftUser with code, cookieApi.getCurrentTime, and the endpoints that create transcripts, cards, QR codes and captchas. The editor completes all of them.
The argument is an object with the endpoint's parameters by name, like { channel_id: "123", limit: 50 } or { Players: true }. The body of a request that sends one goes under body. A call with a wrong parameter fails without being sent or billed.
The result is the JSON the API returns, with its own field names. A member has nick and user.global_name, not the display_name of placeholders.
If a request fails, the promise is rejected with an Error, like discord.getChannel: Unknown Channel (404). Its status is the HTTP status of the response, and for Discord its code is Discord's error code, like 10003 for an unknown channel. Errors from before the request was sent, like a wrong parameter, have neither. Catch the error with try and catch, or the block fails.
try {
return await discord.getGuildMember({ guild_id: input.guild, user_id: input.user });
} catch (e) {
if (e.status === 404) return null;
throw e;
}
Changing things in Discord
The code can send messages, give roles, create channels, ban members and make the other changes the Discord API Request block can. Put what the endpoint takes under body, and the reason that shows in the server's audit log under reason:
const msg = await discord.createMessage({
channel_id: input.channel,
body: { content: `Welcome, ${input.user.mention}!` },
});
await discord.addGuildMemberRole({
guild_id: input.guild,
user_id: input.user.id,
role_id: input.role,
reason: "Finished onboarding",
});
return msg.id;
The code can't reply to the command or button the flow runs with. Return what to say, and reply with a Create response message block after this one.
Reading from Discord is free. Members, roles and channels come from what Kite already knows about the server when it can, so reading them is fast. Each change in Discord and each call of another integration costs 1 credit. The integration has to be enabled in the app. Test runs show the changes instead of making them.
Stored variables
Add the stored variables the code uses under Stored variables, each with the name the code uses for it. The code then reads and changes them by that name:
const total = await variables.increment({ name: "xp", scope: input.user.id, by: 5 });
const top = await variables.list({ name: "xp", limit: 3 });
return `You have ${total} XP. The top is ${top.map((v) => v.value).join(", ")}.`;
variables.get({ name, scope })returns the value, ornullif there's none.variables.set({ name, scope, value })stores a value, which can be text, a number, a list or an object.variables.append({ name, scope, value })addsvalueto the end of a list as one item, or to the end of text, and returns the new value. An object or a list appended to an empty variable or to text starts a list. For a list of text or numbers, set the variable to[]first.variables.prependadds the value at the start, and an empty value adds nothing.variables.increment({ name, scope, by })addsby, 1 if it's left out, and returns the new number.variables.delete({ name, scope })deletes the value.variables.list({ name, scope_prefix, sort, limit, skip })returns values like the List stored variable values block, each withscope,valueandupdated_at.
Runs at the same time don't overwrite each other's append, prepend and increment, unlike a get followed by a set.
A scoped variable needs a scope, like a user ID, and an unscoped one ignores it. The name stays the same when the variable is renamed, and when the flow is part of a template, apps that add it get their own copy of the variable. Getting a value is free, the others cost 1 credit each, like the blocks.
AI
The code can use the AI blocks with ai.ask, ai.check, ai.choose, ai.score and ai.translate. Each call costs what its block costs, so the code can sort or answer several messages in one run. Calls with Promise.all run at the same time:
const spam = await Promise.all(
input.messages.map((m) => ai.check({ question: "Is this spam?", input: m.content }))
);
return input.messages.filter((m, i) => spam[i] > 0.8).map((m) => m.id);
ai.ask({ prompt, system_prompt, model, max_tokens })returns the answer of Ask AI. The model issmall,mediumorlarge, andsmallif it's left out. It costs 5, 20 or 100 credits, and more for long prompts and answers, like the block.ai.check({ question, input })returns how likely the answer to a yes or no question is yes, from 0 to 1, like AI Check.ai.choose({ question, input, options })picks one of the options, which is an object with a description of each, like{ bug: "Something broke", idea: "" }. It returns{ choice, confidence }, withchoiceset tonullif no option fits, like AI Choice.ai.score({ question, input, levels })rates the input with 2 to 10 levels from the lowest to the highest, and returns the level from 1, like AI Score.ai.translate({ text, language })returns{ text, source_language }, like AI Translate.
Check, choose and score cost 1 credit each, and translate 1 credit for each 1,000 characters. Searching the web isn't available, as it takes longer than the 10 seconds a run can take. Use the Search the Web block instead. A run that asks the medium or large model several times can also take too long, so ask those once per run.
Sending requests to other websites
fetch sends a request to any URL, like the Send API Request block. It takes the method, the headers and a text body. The response has ok, status, statusText, headers.get(), text() and json().
const res = await fetch("https://api.example.com/scores", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer {{secrets.API_KEY}}",
},
body: JSON.stringify({ user: input.user.id, points: 5 }),
});
if (!res.ok) throw new Error(`The API answered ${res.status}`);
return await res.json();
{{secrets.NAME}} in the URL, the headers or the body is replaced with the app's secret when the request is sent, so the code never sees its value. Write each secret out in the code like that. A request can only use the secrets the code names, so a name the code puts together, like "{{secrets." + name + "}}", fails. Other placeholders aren't filled in, pass values in as inputs instead.
The body has to be text, so turn objects into JSON with JSON.stringify and set the Content-Type header. Without that header, the body is sent as plain text. Like in a browser, a response with an error status like 404 doesn't throw. Check res.ok or res.status instead. Only a request that gets no response at all throws, like one to a website that doesn't exist. The response can be up to 1 MB, and it's read as text, so images and other files don't come through.
fetch calls http.request({ url, method, headers, body }), which returns { status, status_text, headers, body } with the names of the headers in lowercase. Each request costs 3 credits, like a Send API Request block. Test runs don't send them.
Writing the code
The editor completes the calls, their parameters, the inputs and the stored variables of the block. To write the code in your own editor, like VS Code, download kite.d.ts, put it next to your file and add /// <reference path="kite.d.ts" /> as the file's first line. The editor then knows the calls, what they take and what they return, like the fields of a Discord message. The types of Discord's objects are in the discord namespace, like discord.MessageResponse. Write the code inside async function run() { ... } there, as editors don't allow return and await outside a function, and paste the body into the block.
Limits
The code can be up to 4,000 characters and have up to 20 inputs, which together can be up to 1 MB. It can run for 25 ms and use 8 MB of memory. That's enough to work through a few hundred items, like the messages a List channel messages block returns. The value it returns can be up to 16 KB. It can make 50 calls per run that cost credits, like sending a message, changing or listing stored variables, asking AI, reading from ER:LC or a fetch request. Free calls, like reads from Discord and getting a stored variable, don't count toward the 50. Every call also counts as one of the 100 steps a flow can take in a run, like a block. A run can take 10 seconds in all, and waiting for calls doesn't count against the 25 ms.
If the code throws an error or goes over a limit, the block fails with the error and the line it happened on. An Error handler block can catch it.
The block costs 3 credits each time it runs.