# Get Table Meta API

> Read the columns of a table or the fields of a collection in one common format with the get table meta system API of API Maker - names, types, counts - on every database, for migrations and dynamic code.

Source: https://docs.apimaker.dev/v1/docs/apis-all/system-apis/system-generated-get-table-meta-api.html

Describes a table : the name and the type of every column (or of every field found in a MongoDB collection), in the same format for every database. Migration scripts use it to check whether a column exists before adding it.

| | |
|---|---|
| Method | POST |
| URL | `/api/system-api/admin/get-table-meta` : `admin` is the user path of your account |
| Body | `{ instance, database, collection }` or an array of them |
| Answer | `data` : an array of fields, or an array of arrays |
| From code | [`g.sys.system.getTableMeta`](https://docs.apimaker.dev/v1/examples/sys/system/getTableMeta.html) |

## The call

**Body**

```json
{ "instance": "mysql8", "database": "inventory", "collection": "customers" }
```

**Answer**

```json
{
    "success": true,
    "statusCode": 200,
    "data": [
        { "name": "customer_id", "path": "customer_id", "count": 24, "total_count": 24, "type": "Number", "has_duplicates": false, "probability": 1 },
        { "name": "first_name", "path": "first_name", "count": 24, "total_count": 24, "type": [ "String", "Null" ], "has_duplicates": true, "probability": 1 }
    ]
}
```

| Key | Meaning |
|---|---|
| `name`, `path` | The column, with its path for nested MongoDB fields. |
| `type` | The type, or the list of types seen (MongoDB). |
| `count`, `total_count`, `probability` | For MongoDB, in how many of the sampled documents the field appears. |
| `has_duplicates` | Whether the same value appears twice in the sample. |

- `table` works in place of `collection`. An array of tables answers an array of arrays, in the same order.
- A table of a tenant : `"instance": "crm::acme"`, or the header `x-am-tenant-username`.

## From code

**A migration which adds a column once**

```typescript
const meta = await g.sys.system.getTableMeta({ instance: 'mysql8', database: 'inventory', table: 'order_transactions' });
if (!meta.find(c => c.name === 'description')) {
    await g.sys.system.executeQuery({ instance: 'mysql8', query: 'ALTER TABLE `inventory`.`order_transactions` ADD COLUMN `description` varchar(255) NULL' });
}
```

## Access and settings

- Over HTTP, a system API answers once its [settings](https://docs.apimaker.dev/v1/docs/settings/systemApiSettings.html) give it `apiAccessType: TOKEN_ACCESS` (the token of an API user whose [group](https://docs.apimaker.dev/v1/docs/apis-security/api-group-permission.html) grants this system API, in `x-am-authorization`) or `IS_PUBLIC`. Without settings it is `NO_ACCESS` : your code calls it through `g.sys`, the admin panel tests it, and an HTTP call is refused.
- The settings can also cache the answer or require person tokens (`authProviders`) ; [pre and post hooks](https://docs.apimaker.dev/v1/docs/apis-all/hooks/preHook-api.html) run around it like around any API.
- The [request headers](https://docs.apimaker.dev/v1/docs/apis-all/header/requestHeader.html) apply : `x-am-response-case`, `x-am-content-type-response`, `x-am-internationalization`, `x-am-tenant-username`…

## Errors

| Code | When |
|---|---|
| `400` | The body is wrong : the message names the missing or invalid key. |
| `401` | No valid API user token, or the API is `NO_ACCESS` : `You are not authorized to access this API.` |
| `403` | No group grants this system API. |

## Related

- [All APIs at a glance](https://docs.apimaker.dev/v1/docs/apis-all/overview.html) · [Response format](https://docs.apimaker.dev/v1/docs/apis-all/response-format.html) · [System API settings](https://docs.apimaker.dev/v1/docs/settings/systemApiSettings.html) · [System APIs from code](https://docs.apimaker.dev/v1/examples/sys/system/system.html)
