Skip to content
This page View Markdown Open in ChatGPT Open in Claude

Code examples

Custom APIs, hooks, events, schedulers, migrations, process initializers and test cases all receive the same global object g. Every page of this section is one method of it, with examples you can paste into the editor of the admin panel, in every language : pick yours in the tabs, and every page shows it from then on.

Every piece of code is the function main of its language ; the examples are the inside of it :

1
2
3
4
5
6
7
8
import * as T from 'types';          // the types of API Maker
import * as db from 'db-interfaces'; // the interfaces generated from your schemas

async function main(g: T.IAMGlobal) {
    // your code, with await on every call of g.sys
    return { hello: 'world' };       // what the caller receives in data
}
module.exports = main;
1
2
3
4
5
6
from am import G


async def main(g: G):
    # your code, with await on every call of g.sys (a coroutine ; `def main` works too, without await)
    return {'hello': 'world'}  # what the caller receives in data
1
2
3
4
5
6
<?php
function main(AmG $g)
{
    // your code : every call of $g->sys answers at once
    return ['hello' => 'world']; // what the caller receives in data
}
1
2
3
4
5
6
7
8
file static class Api
{
    public static async Task<object?> Main(G g)
    {
        // your code, with await on every call of g.Sys
        return new { hello = "world" }; // what the caller receives in data
    }
}
1
2
3
4
5
6
7
8
package code

import "apimaker/am"

func Main(g *am.G) (any, error) {
    // your code : every call of g.Sys answers with its error
    return am.M{"hello": "world"}, nil // what the caller receives in data
}
1
2
3
4
5
6
use am::{Errors, G, Value, json};

pub fn main(g: &mut G) -> Result<Value, Errors> {
    // your code : ? after every call of g.sys
    Ok(json!({ "hello": "world" })) // what the caller receives in data
}
1
2
3
4
5
6
7
8
9
import am.G;
import java.util.Map;

class Api {
    static Object main(G g) {
        // your code : every call of g.sys answers at once
        return Map.of("hello", "world"); // what the caller receives in data
    }
}
1
2
3
4
5
6
import am.G

fun main(g: G): Any? {
    // your code : every call of g.sys answers at once
    return mapOf("hello" to "world") // what the caller receives in data
}

Every language gets the same g, written the way the language writes things (g.sys.db.get_all in Python, g.Sys.Db.GetAll in C# and Go) : see Languages and runtimes.

The whole envelope

A g.sys call returns data and throws the errors, so the caller of your API gets them. The whole response envelope, which never throws, comes with true as the second argument in TypeScript (const res = await g.sys.db.count({ ... }, true); if (!res.success) ...), and through full in the other languages (res = await g.sys.db.full.count(...) in Python, g.Sys.Db.Full.Count(...) in C# and Go).