Skip to main content

Documentation

JavaScript Functions

Server-side compute with JavaScript/Boa

JavaScript Functions

⚡ Serverless Compute with JavaScript

Run custom logic safely at the edge.

What are JavaScript Functions?

Mounted under /v1/functions

The functions service and its router are mounted in stackhouse/src/main.rs under /v1/functions. The examples below are real curl commands against a running server.

Stackhouse functions are written in JavaScript and executed by the embedded Boa engine. The runtime resolves a handler in one of three forms:

  • a global handler function
  • exports.handler
  • module.exports

The function receives a single input argument (a JSON value) and should return a JSON-serializable value.

runtime accepts javascript, typescript, wasm_rust, or wasm_js in the request, but every value executes as raw JavaScript via Boa today — the wasm_rust/wasm_js values are recorded for forward compatibility only and do not trigger any WASM compilation or execution.

Quick Example

1. Write a JavaScript function

// process.js
exports.handler = function(input) {
    return { doubled: input.value * 2 };
};

2. Deploy to Stackhouse

curl -X POST http://localhost:3000/v1/functions/deploy \
  -H "Content-Type: application/json" \
  -d '{
    "name": "double",
    "runtime": "javascript",
    "source_code": "exports.handler = (input) => ({ doubled: input.value * 2 });"
  }'

3. Execute

curl -X POST http://localhost:3000/v1/functions/invoke/double \
  -H "Content-Type: application/json" \
  -d '{"value": 21}'

# Returns: {"success": true, "output": {"doubled": 42}}

Use Cases

1. Data Validation

exports.handler = (input) => {
    if (!input.email || !input.email.includes("@")) {
        throw new Error("Invalid email");
    }
    return { valid: true };
};

2. Data Transformation

exports.handler = (input) => {
    return {
        ...input,
        total: input.price * input.quantity
    };
};

3. Business Logic

exports.handler = (input) => {
    if (input.amount > 1000) {
        return input.amount * 0.9;
    }
    return input.amount;
};

API Reference

Deploy Function

POST /v1/functions/deploy
Content-Type: application/json

{
  "name": "myfunc",
  "runtime": "javascript",
  "entrypoint": "handler",
  "source_code": "exports.handler = (input) => input"
}

runtime accepts javascript, typescript, wasm_rust, or wasm_js — all four are run by the Boa JS engine today; only the value is stored differently. entrypoint defaults to handler.

Execute Function

POST /v1/functions/invoke/:name
Content-Type: application/json

{
  "value": 42
}

List Functions

GET /v1/functions

Returns {"success": true, "data": [...]} (field is data, not functions).

Delete Function

DELETE /v1/functions/:id

Returns {"success": true, "message": "Function deleted"}.

Security

JS sandbox security
✅ Memory IsolationBoa engine isolates execution
✅ Resource LimitsCPU time, memory caps
✅ No File SystemCannot read/write files
✅ Timeout EnforcementPrevent infinite loops
✅ Fast ExecutionCompiled JS in the same process

Best Practices

  1. Keep functions small - source code is stored in the database
  2. Use timeouts - default 30s; prevent infinite loops
  3. Limit memory - default 128MB
  4. Handle errors - Return clear error messages
  5. Return JSON-serializable values - Boa serializes the result to JSON

Resources


Ready to deploy functions? Continue to Realtime 🚀