Run JavaScript Code
Description
This endpoint allows users to run JavaScript code with optional imports. The code is executed in a secure environment and the output is returned.
Endpoint
POST /api/v1/run-js-code
Headers
- Content-Type:
application/json
- Authorization: Bearer
<API_KEY>
(required)
Request Body
{
"imports": "string", // Optional. Import statements to be included.
"code": "string" // Required. The JavaScript code to be executed.
}
Responses
Success (200)
Returns the output of the executed JavaScript code.
{
"output": "any" // The result of the executed code
}
- Content-Type: application/json
- X-RateLimit-Limit: The rate limit for the user.
- X-RateLimit-Remaining: The remaining number of requests for the user.
Bad Request (400)
Returned if the request is invalid.
{
"error": "Error message describing the issue"
}
Internal Server Error (500)
Returned if there’s an error during code execution.
{
"error": "Failed to run code: [error message]"
}
Example Request
curl -X POST https://app.dumplingai.com/api/v1/run-js-code \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"imports": "import { format } from 'npm:date-fns';",
"code": "const now = new Date(); return format(now, 'yyyy-MM-dd');"
}'
Example Response
{
"output": "2024-07-17"
}
Notes
- The combined length of
imports
andcode
must not exceed 8192 characters. - This endpoint uses 5 credits per request.
- The code is executed in a secure environment using ValTown’s eval.run function.
- Usage is recorded and events may be logged for analytics purposes if enabled.
Supported Imports
There are thousands of modules written in JavaScript and TypeScript that you can instantly import to extend and supercharge your code. We support importing code from a variety of sources, including NPM, JSR, esm.sh, deno.land. You can also import code from arbitrary URLs if they provide the correct Content-Type
header and contain JavaScript or TypeScript. Imports work the same as in Deno, so you can learn more from their docs.
NPM
To import an npm module, like zod, add npm:
in front of the name:
import { z } from "npm:zod";
NPM compatibility and the sandbox
Most NPM modules are supported and will work great. However, we do run in a security sandbox: a val can’t access the filesystem. Some NPM modules try to do that, and won’t be able to. Using esm.sh is often an effective workaround for these modules.
JSR
We support importing JSR modules. Simply add jsr:
and @
in front of the module name:
import { concat } from "jsr:@std/bytes";
HTTP Imports
To import from a URL, like from esm.sh
, you just specify the full url. For example, to import zod
from esm.sh
:
import { z } from "https://esm.sh/zod";
Node builtins
If you want to import a Node.js built-in module, add node:
in front of its name. For example, to import the crypto module:
import { createHmac } from "node:crypto";
Rate Limiting
Rate limit headers (X-RateLimit-Limit
and X-RateLimit-Remaining
) are included in the response to indicate the user’s current rate limit status.