# Code Examples

> A working example, in TypeScript, Python, PHP, C#, Go, Rust, Java and Kotlin, of 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/v4/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/v4/docs/pre-defined-terms/global-object-g.html). 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](https://docs.apimaker.dev/v4/docs/languages/index.html) : 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 :

**TypeScript**

```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;
```

**Python**

```python
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
```

**PHP**

```php
<?php
function main(AmG $g)
{
    // your code : every call of $g->sys answers at once
    return ['hello' => 'world']; // what the caller receives in data
}
```

**C#**

```csharp
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
    }
}
```

**Go**

```go
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
}
```

**Rust**

```rust
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
}
```

**Java**

```java
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
    }
}
```

**Kotlin**

```kotlin
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](https://docs.apimaker.dev/v4/docs/languages/index.html).

- [g.sys.db](https://docs.apimaker.dev/v4/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/v4/examples/sys/db/getAll.html#without-a-schema) : The same operations without the schema.
- [g.sys.system](https://docs.apimaker.dev/v4/examples/sys/system/system.html) : Encrypt, hash, tokens, secrets, queries, indexes, events, validation, external calls.
- [g.sys.cache](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-set-redis-key-api.html#from-code) : Redis keys and cache resets.
- [g.req](https://docs.apimaker.dev/v4/examples/req/req.html#body) : Body, headers, params, query params, event data and the tokens of the caller.
- [g.res](https://docs.apimaker.dev/v4/examples/res/res.html#output) : Output, status code, errors, warnings, content type.
- [Custom API](https://docs.apimaker.dev/v4/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/v4/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 whole envelope**
>
> A `g.sys` call returns `data` and throws the errors, so the caller of your API gets them. The whole [response envelope](https://docs.apimaker.dev/v4/docs/apis-all/response-format.html), 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).
