# Get Indexes API

> List the indexes of a table on any database of the account in one common format with the get indexes system API of API Maker - name, key fields with their order, unique or not.

Source: https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-get-indexes-api.html

Lists the indexes of a table in the same format for every database : the name, the fields with their direction, whether it is unique, and the access method when the database says it.

| | |
|---|---|
| Method | POST |
| URL | `/api/system-api/admin/get-indexes` : `admin` is the user path of your account |
| Body | `{ instance, database, collection | table }` or an array of them |
| Answer | `data` : an array of indexes, or an array of arrays |
| From code | [`g.sys.system.getIndexes`](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-get-indexes-api.html#from-code) |

## The call

**Body**

```json
{ "instance": "mysql8", "database": "inventory", "table": "orders" }
```

**Answer**

```json
{
    "success": true,
    "statusCode": 200,
    "data": [
        { "name": "PRIMARY", "key": { "id": 1 }, "unique": true },
        { "name": "am_ix_orders_customer_id_status_3f9a1c", "key": { "customer_id": 1, "status": -1 }, "unique": false, "method": "BTREE" }
    ]
}
```

| Property | Meaning |
|---|---|
| `name` | Index name. |
| `key` | The fields in order, with their direction : `1` ascending, `-1` descending, `hashed` or `text` on MongoDB. |
| `unique` | A unique index (the primary key included). |
| `method` | The access method when the database tells it (`BTREE`, `HASH`, `gin`…). |

- An array of tables answers an array of lists, in the same order.

## From code

`g.sys.system.getIndexes` makes the same call from your code, whatever the settings of this API say about HTTP callers.

### The indexes of a table

The indexes of a table, in the same format for every database.

**TypeScript**

```typescript
const indexes = await g.sys.system.getIndexes({
    instance: 'mysql8',
    database: 'inventory',
    table: 'orders',
});
// [
//     { name: 'PRIMARY', key: { id: 1 }, unique: true },
//     { name: 'am_ix_orders_customer_id_status_3f9a1c', key: { customer_id: 1, status: -1 }, unique: false, method: 'BTREE' },
// ]
```

**Python**

```python
indexes = await g.sys.system.get_indexes(
    'mysql8',
    'inventory',
    table='orders',
)
# [
# { name: 'PRIMARY', key: { id: 1 }, unique: true },
# { name: 'am_ix_orders_customer_id_status_3f9a1c', key: { customer_id: 1, status: -1 }, unique: false, method: 'BTREE' },
# ]
```

**PHP**

```php
$indexes = $g->sys->system->getIndexes(
    'mysql8',
    'inventory',
    table: 'orders',
);
// [
// { name: 'PRIMARY', key: { id: 1 }, unique: true },
// { name: 'am_ix_orders_customer_id_status_3f9a1c', key: { customer_id: 1, status: -1 }, unique: false, method: 'BTREE' },
// ]
```

**C#**

```csharp
var indexes = await g.Sys.System.GetIndexes(new GetIndexes
{
    Instance = "mysql8",
    Database = "inventory",
    Table = "orders",
});
// [
// { name: 'PRIMARY', key: { id: 1 }, unique: true },
// { name: 'am_ix_orders_customer_id_status_3f9a1c', key: { customer_id: 1, status: -1 }, unique: false, method: 'BTREE' },
// ]
```

**Go**

```go
indexes, err := g.Sys.System.GetIndexes(am.GetIndexes{
	Instance: "mysql8",
	Database: "inventory",
	Table:    "orders",
})
if err != nil {
	return nil, err
}
// [
// { name: 'PRIMARY', key: { id: 1 }, unique: true },
// { name: 'am_ix_orders_customer_id_status_3f9a1c', key: { customer_id: 1, status: -1 }, unique: false, method: 'BTREE' },
// ]
```

**Rust**

```rust
let indexes = g.sys.system.get_indexes(
    GetIndexes::default()
        .instance("mysql8")
        .database("inventory")
        .table("orders"),
)?;
// [
// { name: 'PRIMARY', key: { id: 1 }, unique: true },
// { name: 'am_ix_orders_customer_id_status_3f9a1c', key: { customer_id: 1, status: -1 }, unique: false, method: 'BTREE' },
// ]
```

**Java**

```java
var indexes = g.sys.system.getIndexes(new GetIndexes()
    .instance("mysql8")
    .database("inventory")
    .table("orders"));
// [
// { name: 'PRIMARY', key: { id: 1 }, unique: true },
// { name: 'am_ix_orders_customer_id_status_3f9a1c', key: { customer_id: 1, status: -1 }, unique: false, method: 'BTREE' },
// ]
```

**Kotlin**

```kotlin
val indexes = g.sys.system.getIndexes(
    GetIndexes()
        .instance("mysql8")
        .database("inventory")
        .table("orders"),
)
// [
// { name: 'PRIMARY', key: { id: 1 }, unique: true },
// { name: 'am_ix_orders_customer_id_status_3f9a1c', key: { customer_id: 1, status: -1 }, unique: false, method: 'BTREE' },
// ]
```

## Access and settings

- Over HTTP, a system API answers once its [settings](https://docs.apimaker.dev/v4/docs/settings/systemApiSettings.html) give it `apiAccessType: TOKEN_ACCESS` (the token of an API user whose [group](https://docs.apimaker.dev/v4/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/v4/docs/apis-all/hooks/preHook-api.html) run around it like around any API.
- The [request headers](https://docs.apimaker.dev/v4/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/v4/docs/apis-all/overview.html) · [Response format](https://docs.apimaker.dev/v4/docs/apis-all/response-format.html) · [System API settings](https://docs.apimaker.dev/v4/docs/settings/systemApiSettings.html) · [System APIs from code](https://docs.apimaker.dev/v4/examples/sys/system/system.html)
