# Get Record by ID API

> Instantly retrieve records by unique ID for fast, targeted data access.

Source: https://docs.apimaker.dev/v1/examples/sys/db/gen/getByIdGen.html

`g.sys.db.gen.getByIdGen` calls the [get by id generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-get-by-id-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Get by id**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    id: 100050,
    primaryKey: 'COLUMN_NAME',
    select: "COLUMN_NAMES"
});
```

## Simple Get by id

**Simple Get by id from table**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 2
});
```

## With header

**Get by id with header**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 2,
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## With header and select

**Get by id with header and select**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 2,
    headers: {
        "x-am-response-case": "capitalCase"
    },
    select:"first_name,last_name"
});
```

## With primary key override

**Get by id with primary key override**

```typescript
let getById= await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "NICK",
    primaryKey:"first_name",
});
```

## With all params

**Get by id with all params**

```typescript
let getAll = await g.sys.db.gen.getByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "NICK",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    select: "first_name,last_name",
    deep: [{ // string, string[], array of objects are supported
        s_key: "customer_id",
        t_key: "owner_id",
        t_col: "products",
        select: "proName,proDescription,proPrice"
    }],
    primaryKey: "first_name"
});
```

## Multi-tenant
- `instance: "crm::acme"` works on the database of the tenant `acme` of the [multi-tenant](https://docs.apimaker.dev/v1/docs/features/multi-tenant.html) instance `crm`.
- With `instance: "crm"`, the call works on the database of the tenant of the request: a custom API called with the [x-am-tenant-username](https://docs.apimaker.dev/v1/docs/apis-all/header/requestHeader.html#x-am-tenant-username) header passes it on to the APIs it calls. A request without a tenant works on the structure database.

**Get the customer 1 of the tenant acme**

```typescript
let getById = await g.sys.db.gen.getByIdGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    id: 1
});
```

## Related

- [Get by id generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-get-by-id-api.html) · [Schema API from code](https://docs.apimaker.dev/v1/examples/sys/db/getById.html) · [The global object g](https://docs.apimaker.dev/v1/docs/pre-defined-terms/global-object-g.html)
