# 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`](https://docs.apimaker.dev/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
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;
```

- [g.sys.db](https://docs.apimaker.dev/v1/examples/sys/db/getAll.html) : The 17 schema APIs of a table, from get all to distinct with query.
- [g.sys.db.gen](https://docs.apimaker.dev/v1/examples/sys/db/gen/getAllGen.html) : The same 17 operations without the schema.
- [g.sys.system](https://docs.apimaker.dev/v1/examples/sys/system/system.html) : Encrypt, hash, tokens, secrets, queries, indexes, events, validation, external calls.
- [g.sys.cache](https://docs.apimaker.dev/v1/examples/sys/cache/setKey.html) : Redis keys and cache resets.
- [g.req](https://docs.apimaker.dev/v1/examples/req/body/reqBody.html) : Body, headers, params, query params, event data and the tokens of the caller.
- [g.res](https://docs.apimaker.dev/v1/examples/res/output/output.html) : Output, status code, errors, warnings, content type.
- [Custom API](https://docs.apimaker.dev/v1/examples/custom-apis/custom-api.html) : A complete custom API, from the settings to the code.
- [g.logger and g.shared](https://docs.apimaker.dev/v1/docs/pre-defined-terms/global-object-g.html#glogger-and-gshared) : Logs which reach the caller and the log table, and values shared between hooks.

> **Tip : The second argument**
>
> Every `g.sys` call takes an optional second argument. With `true`, the call returns the whole [response envelope](https://docs.apimaker.dev/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.
