# TenantService

Accessible via `iam.tenants`. Manages multi-tenancy.

> **Authentication:** All `/tenants/**` endpoints require the **system API key** (`ROLE_SYSTEM`). Bearer tokens from user login are **not** accepted. You must set the `apiKey` option when creating the client. See the [README.md Authentication section](../README.md#authentication) for details.

## Search Tenants

```typescript
const result = await iam.tenants.search({ keyword: 'test' });

// Access results
result.tenants.content; // Tenant[]
result.tenants.totalElements; // Total number of results
result.tenants.totalPages; // Total number of pages
result.tenants.number; // Current page
result.tenants.size; // Page size
```

### Search parameters

| Parameter  | Type                    | Description            |
| ---------- | ----------------------- | ---------------------- |
| `keyword`  | `string`                | Free-text search       |
| `pageable` | `{ page, rowsPerPage }` | Pagination (0-indexed) |

## Get Single Tenant

```typescript
const { tenant } = await iam.tenants.getById('tenant-id');
const { tenant } = await iam.tenants.getByCode('tenant-code');
```

## Register Tenant

```typescript
const { id } = await iam.tenants.register({
  code: 'my-tenant',
  name: 'My Tenant',
  roles: ['ROLE_USER'], // optional
  metadata: { region: 'EU' }, // optional
  domains: ['example.com'] // optional
});
```

## Import Tenant

Creates a tenant with an optional predefined ID.

```typescript
await iam.tenants.importTenant({
  id: 'custom-id', // optional
  code: 'imported',
  name: 'Imported Tenant',
  roles: ['ROLE_USER'], // optional
  metadata: {}, // optional
  domains: [], // optional
  apiKey: 'tenant-api-key' // optional
});
```

## Update Tenant

```typescript
await iam.tenants.update({
  tenantId: 'tenant-id',
  name: 'New Name', // optional
  roles: ['ROLE_USER'], // optional
  metadata: { region: 'US' }, // optional
  domains: ['newdomain.com'] // optional
});
```

## Delete Tenant

```typescript
await iam.tenants.delete('tenant-id');
```
