API Maker

The backend engine for the AI era

Your code

Utility Classes & Process Initializers

Write shared code once and import it everywhere. Run setup code as soon as a sandbox starts.

A utility class is code your custom APIs, hooks, events, schedulers and tests of the same language import: by its path in TypeScript, the way each language imports in the others. A process initializer is code that runs when a sandbox starts, before any request, to prepare what your code needs, like a subscription to Redis or MQTT.

See it live

Step through it, slow it down, or open the full canvas.

api-maker/features/utility-classesLive
  • Import
  • Sandbox
  • Ready
  • New version
Copies
1of the code
Importers
0use it
Active
v2version
Initializers
0runs

Shared code in a utility class. Give it a folder and a name, write it in the language of the code which uses it (TypeScript here) and export what that code needs. Price rules, formatting and API clients live in one place.

How it works

  1. Shared code in a utility class.

    Give it a folder and a name, write it in the language of the code which uses it (TypeScript here) and export what that code needs. Price rules, formatting and API clients live in one place.

  2. Import it by its folder and name.

    Custom APIs, hooks, event listeners, schedulers and test cases import it like a module, with autocompletion in the editor.

  3. Keep versions, activate one.

    Save a new version and activate it: every importer uses it at once, on every server. Go back by activating the old one.

  4. Setup code for every sandbox.

    When a sandbox starts, the active process initializers run one after another, in your order, before it takes any request.

  5. Ready before the first call.

    Ten seconds after API Maker starts it creates the sandboxes of accounts with initializers. A restarted sandbox runs them again: make them safe to run twice.

What you get

Write it once

Price rules, formatting, API clients and validation helpers live in one place instead of being copied into every API.

Listen to other systems

An initializer can connect to a message broker or a Redis channel and react to its messages, for as long as the sandbox lives.

A class per language

Python code imports the classes written in Python, Go code the ones written in Go, and so on. Code of another language reaches what a class does through a custom API.

An example

One tax rule for the whole shop

The checkout API, the invoice scheduler and the order hooks all need the same tax calculation. It lives in a utility class, and when the rate changes you save one new version.

A utility classutils/Calculator.ts
class Calculator {    add(x: number, y: number) {        return x + y;    }    multiply(x: number, y: number) {        return x * y;    }}let temp = new Calculator();export = temp;
Used in a custom APIcustom-api.ts
import * as T from 'types';import * as calc from 'utils/Calculator';async function main(g: T.IAMGlobal) {    return { sum: calc.add(3, 6), product: calc.multiply(3, 6) };}module.exports = main;

Watch it

On the real admin panel, with real data.

Utility Classes and Process Initializers: Shared Code, Loaded Once 9 chapters and the transcript

Good to know

  • An initializer runs again every time a sandbox of its language is created or replaced: make it safe to run more than once.

Questions

What is the difference between an initializer and a scheduler?

A scheduler runs on its intervals, once in the cluster. An initializer runs once in every new sandbox, without any trigger, to set that sandbox up.

Can utility classes use g?

Yes, when the importer passes it: call your functions with g from the custom API, hook or scheduler that uses them.