← Recipe: call a server function

4. Create backend/handlers.mjs

generated Luau client · trusted JavaScript handler · about 25 minutes
trusted validation and result

The handler validates untrusted input again and returns exactly the declared result shape.

When to use it

Create this trusted local-backend module. Keep secrets and privileged provider calls here, never in generated or handwritten client Luau.

  • The operation key must exactly match the declaration.
  • context.reject produces a bounded failure code and message for app.resolve.
  • Luastra validates the returned object against the declared result before it crosses the RPC boundary.

Focused example

export function createHandlers() {
  return Object.freeze({
    async "greeting.message.v1"(input, context) {
      const name = input.name.trim();
      const bytes = new TextEncoder().encode(name).byteLength;
      if (bytes < 2 || bytes > 60) {
        context.reject("VALIDATION", "Name must contain 2 to 60 bytes");
      }
      return { message: `Hello, ${name}!` };
    },
  });
}

Complete inventory

View all pages in Recipe: call a server function
← Previous: 3. Create backend/functions.jsonNext: 5. Generate the typed Luau client →