# Code Examples

> A working TypeScript snippet for every method your code can call in API Maker - g.sys.db and g.sys.db.gen for the database APIs, g.sys.system, g.sys.cache, the request and response objects, the shared space and the logger.

Source: https://docs.apimaker.dev/v1/examples/index.html

Custom APIs, hooks, events, schedulers, migrations, process initializers and test cases all receive the same [global object `g`](/v1/docs/pre-defined-terms/global-object-g.html). Every page of this section is one method of it, with a snippet you can paste into the editor of the admin panel.

Every snippet has the same shape :

```typescript linenums="1"
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;
```

    <a class="am-card am-card--green" href="/v1/examples/sys/db/getAll.html">dbg.sys.dbThe 17 schema APIs of a table, from get all to distinct with query.</a>
    <a class="am-card" href="/v1/examples/sys/db/gen/getAllGen.html">geng.sys.db.genThe same 17 operations without the schema.</a>
    <a class="am-card am-card--purple" href="/v1/examples/sys/system/system.html">sysg.sys.systemEncrypt, hash, tokens, secrets, queries, indexes, events, validation, external calls.</a>
    <a class="am-card am-card--amber" href="/v1/examples/sys/cache/setKey.html">⚡g.sys.cacheRedis keys and cache resets.</a>
    <a class="am-card" href="/v1/examples/req/body/reqBody.html">reqg.reqBody, headers, params, query params, event data and the tokens of the caller.</a>
    <a class="am-card" href="/v1/examples/res/output/output.html">resg.resOutput, status code, errors, warnings, content type.</a>
    <a class="am-card am-card--purple" href="/v1/examples/custom-apis/custom-api.html">&lt;/&gt;Custom APIA complete custom API, from the settings to the code.</a>
    <a class="am-card" href="/v1/docs/pre-defined-terms/global-object-g.html#glogger-and-gshared">≡g.logger and g.sharedLogs which reach the caller and the log table, and values shared between hooks.</a>

!!! tip "The second argument"
    Every `g.sys` call takes an optional second argument. With `true`, the call returns the whole [response envelope](/v1/docs/apis-all/response-format.html) and never throws : `const res = await g.sys.db.count({ ... }, true); if (!res.success) ...`. Without it, the call returns `data` and throws the errors, so the caller of your API gets them.
