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

The code in every language

Each example is the inside of main, written in every language API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (GetAll, DeepItem…) are those of am : from am import GetAll in Python, use am::GetAll; in Rust, import am.GetAll; in Java and Kotlin ; PHP and C# have them without an import, Go names them am.GetAll.

The answer : g.res

Custom APIs and hooks shape the answer of the request through g.res.

Output

g.res.output = { hello: 'world' };
g.res.output = {'hello': 'world'}
$g->res->output = ['hello' => 'world'];
g.Res.Output = new JsonObject { ["hello"] = "world" };
g.Res.SetOutput(am.M{"hello": "world"})
g.res.set_output(json!({ "hello": "world" }));
g.res.setOutput(Map.of("hello", "world"));
g.res.output = mapOf("hello" to "world")

Response

{
    "success": true,
    "statusCode": 200,
    "data": {
        "hello": "world"
    }
}

Status code

statusCode use to set/get status code of the response.

g.res.statusCode = T.EStatusCode.BAD_REQUEST;
g.res.status_code = 400  # BAD_REQUEST
$g->res->statusCode = 400; // BAD_REQUEST
g.Res.StatusCode = 400; // BAD_REQUEST
g.Res.SetStatusCode(400) // BAD_REQUEST
g.res.set_status_code(400); // BAD_REQUEST
g.res.setStatusCode(400); // BAD_REQUEST
g.res.statusCode = 400 // BAD_REQUEST

Response

{
    "success": false,
    "statusCode": 400,
    "data": 400
}

All supported status code.

OK = 200,
NO_CONTENT = 204,
CREATED = 201,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
RESOURCE_NOT_FOUND = 404,
INTERNAL_SERVER_ERROR = 500

Errors

g.res.errors = [{ code: 400, message: "Error message here." }];
g.res.errors = [{'code': 400, 'message': 'Error message here.'}]
$g->res->errors = [['code' => 400, 'message' => 'Error message here.']];
g.Res["errors"] = new JsonArray(new JsonObject { ["code"] = 400, ["message"] = "Error message here." });
g.Res.M["errors"] = []any{am.M{"code": 400, "message": "Error message here."}}
*g.res.errors_mut() = json!([{ "code": 400, "message": "Error message here." }]);
g.res.setErrors(List.of(new ResponseError(Map.of("code", 400, "message", "Error message here."))));
g.res.errors = listOf(ResponseError(mapOf("code" to 400, "message" to "Error message here.")))

Response

{
    "success": false,
    "statusCode": 400,
    "errors": [
        {
            "code": 400,
            "message": "Error message here."
        }
    ],
    "warnings": []
}

Warnings

g.res.warnings = [{ code: 401, message: "This is warning!" }];
return { hello: 'v2' };
g.res.warnings = [{'code': 401, 'message': 'This is warning!'}]
return {'hello': 'v2'}
$g->res->warnings = [['code' => 401, 'message' => 'This is warning!']];
return ['hello' => 'v2'];
g.Res["warnings"] = new JsonArray(new JsonObject { ["code"] = 401, ["message"] = "This is warning!" });
return new { hello = "v2" };
g.Res.M["warnings"] = []any{am.M{"code": 401, "message": "This is warning!"}}
return am.M{"hello": "v2"}, nil
*g.res.warnings_mut() = json!([{ "code": 401, "message": "This is warning!" }]);
Ok(json!({ "hello": "v2" }))
g.res.setWarnings(List.of(new ResponseError(Map.of("code", 401, "message", "This is warning!"))));
return Map.of("hello", "v2");
g.res.warnings = listOf(ResponseError(mapOf("code" to 401, "message" to "This is warning!")))
return mapOf("hello" to "v2")

Response

{
    "success": true,
    "statusCode": 200,
    "data": {
        "hello": "v2"
    },
    "warnings": [
        {
            "code": 401,
            "message": "This is warning!"
        }
    ]
}

Shared between hooks and code

g.res.shared is used to share data between post-hooks, pre-hooks, and code within that API.

g.res.shared.name = 'Joe';
return g.res.shared.name;
g.res.shared.name = 'Joe'
return g.res.shared.name
$g->res->shared->name = 'Joe';
return $g->res->shared->name;
g.Res.Shared["name"] = "Joe";
return g.Res.Shared.GetString("name");
g.Res.Shared()["name"] = "Joe"
return g.Res.Shared().String("name"), nil
g.res.shared_mut()["name"] = json!("Joe");
Ok(g.res.shared()["name"].clone())
g.res.getShared().put("name", "Joe");
return g.res.getShared().getString("name");
g.res.shared["name"] = "Joe"
return g.res.shared.getString("name")

Response

{
    "success": true,
    "statusCode": 200,
    "data": "Joe"
}

Content types

  • The user does not need to set the response content type to send JSON as a response.
  • If the response content type is not set that means it is application/JSON.

If ContentType is set to below system will return data accordingly.

JSON = 'application/json'
XML = 'text/xml'
YAML = 'text/yaml'
TEXT = 'text/plain'
HTML = 'text/html'

Note: If content type is set and not from above list, then API Maker will consider a text the code returns as base64 encoded. Bytes the code returns (a Buffer, bytes, byte[], []byte…) are sent as they are, whatever the content type : see OCTET_STREAM below.

HTML

Content type HTML
g.res.contentType = T.EContentType.HTML;
return "<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>";
Content type HTML
g.res.content_type = 'text/html'
return '<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>'
Content type HTML
$g->res->contentType = 'text/html';
return '<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>';
Content type HTML
g.Res.ContentType = "text/html";
return "<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>";
Content type HTML
g.Res.SetContentType("text/html")
return "<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>", nil
Content type HTML
g.res.set_content_type("text/html");
Ok(json!("<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>"))
Content type HTML
g.res.setContentType("text/html");
return "<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>";
Content type HTML
g.res.contentType = "text/html"
return "<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>"

Response

<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>

JSON

Content type JSON
1
2
3
4
g.res.contentType = T.EContentType.JSON;
g.res.errors = [{ code: 401, message: "This is error" }];
g.res.statusCode = 555;
return "World";
Content type JSON
1
2
3
4
g.res.content_type = 'application/json'
g.res.errors = [{'code': 401, 'message': 'This is error'}]
g.res.status_code = 555
return 'World'
Content type JSON
1
2
3
4
$g->res->contentType = 'application/json';
$g->res->errors = [['code' => 401, 'message' => 'This is error']];
$g->res->statusCode = 555;
return 'World';
Content type JSON
1
2
3
4
g.Res.ContentType = "application/json";
g.Res["errors"] = new JsonArray(new JsonObject { ["code"] = 401, ["message"] = "This is error" });
g.Res.StatusCode = 555;
return "World";
Content type JSON
1
2
3
4
g.Res.SetContentType("application/json")
g.Res.M["errors"] = []any{am.M{"code": 401, "message": "This is error"}}
g.Res.SetStatusCode(555)
return "World", nil
Content type JSON
1
2
3
4
g.res.set_content_type("application/json");
*g.res.errors_mut() = json!([{ "code": 401, "message": "This is error" }]);
g.res.set_status_code(555);
Ok(json!("World"))
Content type JSON
1
2
3
4
g.res.setContentType("application/json");
g.res.setErrors(List.of(new ResponseError(Map.of("code", 401, "message", "This is error"))));
g.res.setStatusCode(555);
return "World";
Content type JSON
1
2
3
4
g.res.contentType = "application/json"
g.res.errors = listOf(ResponseError(mapOf("code" to 401, "message" to "This is error")))
g.res.statusCode = 555
return "World"

Response

{
    "success": false,
    "statusCode": 555,
    "data": "World",
    "errors": [
        {
            "code": 401,
            "message": "This is error"
        }
    ],
    "warnings": []
}

Bytes : application/octet-stream

Content type OCTET_STREAM
g.res.contentType = T.EContentType.OCTET_STREAM;
Content type OCTET_STREAM
g.res.content_type = 'application/octet-stream'
Content type OCTET_STREAM
$g->res->contentType = 'application/octet-stream';
Content type OCTET_STREAM
g.Res.ContentType = "application/octet-stream";
Content type OCTET_STREAM
g.Res.SetContentType("application/octet-stream")
Content type OCTET_STREAM
g.res.set_content_type("application/octet-stream");
Content type OCTET_STREAM
g.res.setContentType("application/octet-stream");
Content type OCTET_STREAM
g.res.contentType = "application/octet-stream"
  • It will download the image.
base64
g.res.contentType = 'image/gif' as T.EContentType;
return 'data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=';
base64
g.res.content_type = 'image/gif'
return 'data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA='
base64
$g->res->contentType = 'image/gif';
return 'data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=';
base64
g.Res.ContentType = "image/gif";
return "data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=";
base64
g.Res.SetContentType("image/gif")
return "data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=", nil
base64
g.res.set_content_type("image/gif");
Ok(json!("data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA="))
base64
g.res.setContentType("image/gif");
return "data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=";
base64
g.res.contentType = "image/gif"
return "data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA="
  • It will send the bytes as they are : a file the code read or downloaded, a PDF it made. Return the bytes (a Buffer, bytes, byte[], []byte), or leave them in g.res.output : the answer is those bytes, under the content type the code names, whatever it is (image/png, application/pdf…). Headers of the code go with them (Content-Disposition, Cache-Control).
bytes
1
2
3
4
const picture = Buffer.from('R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=', 'base64');
g.res.contentType = 'image/gif' as T.EContentType;
g.res.headers = { 'Content-Disposition': 'inline; filename="pixel.gif"' };
return picture;
bytes
1
2
3
4
5
6
import base64

picture = base64.b64decode('R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=')
g.res.content_type = 'image/gif'
g.res.headers = {'Content-Disposition': 'inline; filename="pixel.gif"'}
return picture
bytes
1
2
3
4
5
// A text of PHP holds bytes too : AmBytes says that these are bytes.
$picture = base64_decode('R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=');
$g->res->contentType = 'image/gif';
$g->res->headers = ['Content-Disposition' => 'inline; filename="pixel.gif"'];
return new AmBytes($picture);
bytes
1
2
3
4
var picture = Convert.FromBase64String("R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=");
g.Res.ContentType = "image/gif";
g.Res.Headers = new JsonObject { ["Content-Disposition"] = "inline; filename=\"pixel.gif\"" };
return picture;
bytes
1
2
3
4
5
6
7
8
9
import "encoding/base64"

picture, err := base64.StdEncoding.DecodeString("R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=")
if err != nil {
    return nil, err
}
g.Res.SetContentType("image/gif")
g.Res.SetHeaders(am.M{"Content-Disposition": `inline; filename="pixel.gif"`})
return picture, nil
bytes
1
2
3
4
5
// The bytes of the picture, as they are : a value of JSON has nothing which stands for bytes, `am::bytes` does.
let picture = b"GIF89a\x01\0\x01\0\x80\0\0\xff\xff\xff\xff\xff\xff!\xf9\x04\x01\n\0\x01\0,\0\0\0\0\x01\0\x01\0\0\x02\x02L\x01\0;\0";
g.res.set_content_type("image/gif");
g.res.set_headers(json!({ "Content-Disposition": "inline; filename=\"pixel.gif\"" }));
Ok(am::bytes(picture))
bytes
1
2
3
4
byte[] picture = Base64.getDecoder().decode("R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=");
g.res.setContentType("image/gif");
g.res.setHeaders(Map.of("Content-Disposition", "inline; filename=\"pixel.gif\""));
return picture;
bytes
1
2
3
4
val picture = java.util.Base64.getDecoder().decode("R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=")
g.res.contentType = "image/gif"
g.res.setHeaders(mapOf("Content-Disposition" to "inline; filename=\"pixel.gif\""))
return picture

Text

Content type TEXT
g.res.contentType = T.EContentType.TEXT;
return "Hello world from the API Maker.";
Content type TEXT
g.res.content_type = 'text/plain'
return 'Hello world from the API Maker.'
Content type TEXT
$g->res->contentType = 'text/plain';
return 'Hello world from the API Maker.';
Content type TEXT
g.Res.ContentType = "text/plain";
return "Hello world from the API Maker.";
Content type TEXT
g.Res.SetContentType("text/plain")
return "Hello world from the API Maker.", nil
Content type TEXT
g.res.set_content_type("text/plain");
Ok(json!("Hello world from the API Maker."))
Content type TEXT
g.res.setContentType("text/plain");
return "Hello world from the API Maker.";
Content type TEXT
g.res.contentType = "text/plain"
return "Hello world from the API Maker."

Response

Hello world from the API Maker.

XML

Content type XML
g.res.contentType = T.EContentType.XML;
return "Hello world from the API Maker.";
Content type XML
g.res.content_type = 'text/xml'
return 'Hello world from the API Maker.'
Content type XML
$g->res->contentType = 'text/xml';
return 'Hello world from the API Maker.';
Content type XML
g.Res.ContentType = "text/xml";
return "Hello world from the API Maker.";
Content type XML
g.Res.SetContentType("text/xml")
return "Hello world from the API Maker.", nil
Content type XML
g.res.set_content_type("text/xml");
Ok(json!("Hello world from the API Maker."))
Content type XML
g.res.setContentType("text/xml");
return "Hello world from the API Maker.";
Content type XML
g.res.contentType = "text/xml"
return "Hello world from the API Maker."

Response

<?xml version='1.0'?>
<root>
    <success>true</success>
    <statusCode>200</statusCode>
    <data>Hello world from the API Maker.</data>
</root>

YAML

Content type YAML
g.res.contentType = T.EContentType.YAML;
return "Hello world from the API Maker.";
Content type YAML
g.res.content_type = 'text/yaml'
return 'Hello world from the API Maker.'
Content type YAML
$g->res->contentType = 'text/yaml';
return 'Hello world from the API Maker.';
Content type YAML
g.Res.ContentType = "text/yaml";
return "Hello world from the API Maker.";
Content type YAML
g.Res.SetContentType("text/yaml")
return "Hello world from the API Maker.", nil
Content type YAML
g.res.set_content_type("text/yaml");
Ok(json!("Hello world from the API Maker."))
Content type YAML
g.res.setContentType("text/yaml");
return "Hello world from the API Maker.";
Content type YAML
g.res.contentType = "text/yaml"
return "Hello world from the API Maker."

Response

success: true
statusCode: 200
data: Hello world from the API Maker.