# Instance & Users

> Source: https://agilitycms.com/docs/javascript/management-sdk/instance

The Management SDK exposes instance-level operations: the users who have access to an instance, the locales configured on it, the currently authenticated user, and whether your recent changes have reached the Fetch API yet.

Instance users are managed through `instanceUserMethods` in JavaScript and `client.InstanceUsers` in .NET. Locales, the current user, and Fetch API status are in both SDKs too, and every one of them also has a REST endpoint. Each section below gives the methods and the endpoint.

Every method takes the instance `guid` so a single authenticated client can operate across multiple instances. In .NET it is always the first argument.

> The .NET examples on this page use `Agility.Management.SDK` 2.0. If you are on 1.x, see [Migrating to 2.0](https://agilitycms.com/docs/dotNet/management-sdk-dotnet-migrating-to-2) for the method names.

## Instance users

Access via `apiClient.instanceUserMethods` (JavaScript) or `client.InstanceUsers` (.NET).

| Operation | JavaScript | .NET |
| --- | --- | --- |
| List all users on the instance | `getUsers(guid)` | `GetUsersAsync(guid)` |
| Create a user or update their roles | `saveUser(...)` | `SaveUserAsync(guid, ...)` |
| Remove a user from the instance | `deleteUser(userID, guid)` | `DeleteUserAsync(guid, userId)` |

### Get users

Retrieve all users who have access to the instance.

<div class="code-tabs" data-tabs="JavaScript,.NET">

```ts
const users = await apiClient.instanceUserMethods.getUsers(guid);
console.log(users);
```

```csharp
var users = await client.InstanceUsers.GetUsersAsync(guid);

foreach (var user in users)
{
    Console.WriteLine($"{user.EmailAddress} - {user.FirstName} {user.LastName}");
}
```

</div>

.NET signature: `Task<List<WebsiteUser>> GetUsersAsync(string instanceGuid, CancellationToken cancellationToken = default)`

### Save user

Create a user or update an existing one. If the email address already exists on the instance, the user's roles are updated rather than a new user being created.

<div class="code-tabs" data-tabs="JavaScript,.NET">

```ts
const savedUser = await apiClient.instanceUserMethods.saveUser(
  'user@example.com',
  [/* InstanceRole[] */],
  guid,
  'First',
  'Last'
);
```

```csharp
using Agility.Management.Sdk.Models;

IReadOnlyList<InstanceRole> roles = [/* InstanceRole objects */];

var savedUser = await client.InstanceUsers.SaveUserAsync(
    guid,
    emailAddress: "user@example.com",
    roles: roles,
    firstName: "Jane",
    lastName: "Doe"
);

Console.WriteLine($"Saved user ID: {savedUser?.UserID}");
```

</div>

.NET signature: `Task<InstanceUser> SaveUserAsync(string instanceGuid, string emailAddress, IReadOnlyList<InstanceRole> roles, string firstName = null, string lastName = null, CancellationToken cancellationToken = default)`

| Parameter | Type | Description |
| --- | --- | --- |
| `emailAddress` | string | The user's email address. Used as the match key for existing users. |
| `roles` | `InstanceRole[]` / `IReadOnlyList<InstanceRole>` | The roles to assign on this instance, e.g. `Editor`. |
| `guid` | string | The instance GUID. |
| `firstName` | string, optional | The user's first name. |
| `lastName` | string, optional | The user's last name. |

### Delete user

Remove a user from the instance by their user ID.

<div class="code-tabs" data-tabs="JavaScript,.NET">

```ts
const deletedUser = await apiClient.instanceUserMethods.deleteUser(1234, guid);
```

```csharp
// Returns no value. A failure throws AgilityManagementException.
await client.InstanceUsers.DeleteUserAsync(guid, userId);
```

</div>

.NET signature: `Task DeleteUserAsync(string instanceGuid, int userId, CancellationToken cancellationToken = default)`

---

## Locales

Both SDKs can read the instance's locales.

<div class="code-tabs" data-tabs="JavaScript,.NET">

```ts
// Get the locales configured for an instance
const locales = await apiClient.instanceMethods.getLocales(guid);
console.log(locales);
```

```csharp
// Get the locales configured for an instance
var locales = await client.Locales.GetLocalesAsync(guid);
```

</div>

> REST: `GET /api/v1/instance/{guid}/locales`.

Each locale looks like this:

| Field | Type | Description |
| --- | --- | --- |
| `localeID` | number | Internal identifier, used by the enable/disable endpoints. |
| `localeName` | string | Display name, e.g. `English (United States)`. |
| `localeCode` | string | The code you pass as `locale` everywhere else, e.g. `en-us`. |
| `enabled` | boolean | Whether content can be authored in this locale. |
| `custom` | boolean | Whether it's a custom locale rather than a standard one. |
| `sortOrder` | number | Position in the CMS locale switcher. |

> The JavaScript SDK types this return value loosely, so treat the result as the endpoint's own JSON rather than relying on the declared type.

### Managing locales over REST

The JavaScript SDK wraps only `getLocales`. From JavaScript, call the other endpoints directly with the same bearer token. The .NET SDK has a method on `client.Locales` for every one of them:

| Operation | Endpoint | .NET |
| --- | --- | --- |
| List locales | `GET /api/v1/instance/{guid}/locales` | `GetLocalesAsync(guid)` |
| List enabled **and** disabled locales | `GET /api/v1/instance/{guid}/locales/all` | `GetAllLocalesAsync(guid)` |
| Get one locale by ID | `GET /api/v1/instance/{guid}/locales/{localeId}` | `GetLocaleAsync(guid, localeId)` |
| Add a locale | `POST /api/v1/instance/{guid}/locales` | `SaveLocaleAsync(guid, locale)` |
| Reorder locales | `POST /api/v1/instance/{guid}/locales/sort-order` | `SetSortOrderAsync(guid, orderedLocaleIds)` |
| Enable a locale | `PATCH /api/v1/instance/{guid}/locales/{localeId}/enable` | `EnableLocaleAsync(guid, localeId)` |
| Disable a locale | `PATCH /api/v1/instance/{guid}/locales/{localeId}/disable` | `DisableLocaleAsync(guid, localeId)` |

`/locales/all` returns two arrays, `enabledLocales` and `disabledLocales`, which is the call you want when building a locale-management UI.

There's no endpoint for getting a locale by its code. To look one up, read `/locales/all` and filter on `localeCode`, as the example below does.

> **There is no delete.** Locales are enabled and disabled, never removed — which is the right design, since deleting one would orphan every piece of content authored in it. Disabling hides a locale from authoring without touching the content already there.

```ts
async function enableLocale(guid: string, localeCode: string, token: string) {
  const base = 'https://mgmt.aglty.io/api/v1/instance';
  const headers = { Authorization: `Bearer ${token}` };

  const all = await fetch(`${base}/${guid}/locales/all`, { headers }).then(r => r.json());
  const match = all.disabledLocales?.find(l => l.localeCode === localeCode);

  if (!match) {
    console.log(`${localeCode} is not in the disabled list — already enabled, or not added yet`);
    return;
  }

  await fetch(`${base}/${guid}/locales/${match.localeID}/enable`, {
    method: 'PATCH',
    headers,
  });
}
```

---

## Current user

Get the currently authenticated user along with all of their instance access details — useful for confirming which instances a token can actually reach.

```ts
const user = await apiClient.serverUserMethods.me(guid);
```

> .NET: `client.ServerUsers.GetCurrentUserAsync()`. REST: `GET /api/v1/users/me`.

> **Pass your real `guid`, even though the endpoint isn't instance-scoped.** The JavaScript SDK uses the GUID's regional suffix to choose which API host to call. Hand it an empty string and it falls back to the default region, which returns nothing useful if your instance lives in Canada, Europe, or Australia.

---

## Fetch API sync status

This one solves a real timing problem. The Management API writes to the CMS, but content is served from the Fetch API's CDN, and there's a propagation delay between the two. If a deploy script publishes content and then immediately rebuilds a site, it can build against content that hasn't landed yet.

`getFetchApiStatus` tells you where that sync is:

```ts
// mode: 'fetch' for the live CDN, 'preview' for the preview CDN
const status = await apiClient.instanceMethods.getFetchApiStatus(guid, 'fetch');

if (status.inProgress) {
  console.log(`Sync running since ${status.startTime}, ${status.itemsAffected} items affected`);
} else {
  console.log(`Last sync completed ${status.completionTime}`);
}
```

**Signature:** `getFetchApiStatus(guid: string, mode?: FetchApiSyncMode, waitForCompletion?: boolean): Promise<FetchApiStatus>`

Pass `waitForCompletion: true` to have the SDK poll until the sync finishes rather than returning the current snapshot — the polling interval and attempt cap come from the `duration` and `retryCount` fields on `Options`.

| Field | Description |
| --- | --- |
| `inProgress` | Whether a sync is running right now. |
| `itemsAffected` | Number of items in the sync. |
| `startTime` / `completionTime` | When the sync started and finished. |
| `errorMessage` | Set if the sync failed. |
| `pushType` | `1` = Preview, `2` = Fetch/Live. |
| `lastContentVersionID` | Last content version synced — compare against a save's version to confirm it landed. |
| `lastDeletedContentVersionID` | Last deleted content version synced. |
| `lastDeletedPageVersionID` | Last deleted page version synced. |
| `maxChangeDate` | Most recent change date processed. |
| `maxContentModelDate` | Most recent model change processed. |
| `leaseID` | Lease identifier for the sync operation. |
| `websiteName` | The instance's website name. |
| `timestamp` | When this status record was written. |

> .NET: `client.SyncStatus.GetFetchApiStatusAsync(guid, SyncMode.Fetch)`, or `client.SyncStatus.WaitForFetchApiSyncAsync(guid, SyncMode.Fetch, timeout)` to wait for the sync to finish. REST: `GET /api/v1/instance/{guid}/fetch-api-status?mode=fetch` (`mode` accepts `fetch` or `preview`).

**Wait for a publish to go live before rebuilding:**

```ts
async function waitForSync(guid: string, timeoutMs = 120_000) {
  const deadline = Date.now() + timeoutMs;

  while (Date.now() < deadline) {
    const status = await apiClient.instanceMethods.getFetchApiStatus(guid, 'fetch');

    if (status.errorMessage) {
      throw new Error(`Fetch API sync failed: ${status.errorMessage}`);
    }
    if (!status.inProgress) {
      return status;
    }

    await new Promise(r => setTimeout(r, 3000));
  }

  throw new Error('Timed out waiting for the Fetch API sync');
}
```

---

## Retrieving API keys programmatically

The Fetch and Preview API keys are visible in the Agility UI, but you can also read them with an authenticated Management API call — handy when provisioning environments or seeding CI secrets rather than copying keys by hand.

| Key | Endpoint |
| --- | --- |
| Fetch (live content) | `GET /oauth/getfetchkey?guid={guid}` |
| Preview (staged content) | `GET /oauth/getpreviewkey?guid={guid}` |

Both return the key as a plain string and both require a valid Management API bearer token.

```ts
async function getApiKeys(guid: string, token: string) {
  const headers = { Authorization: `Bearer ${token}` };

  const [fetchKey, previewKey] = await Promise.all([
    fetch(`https://mgmt.aglty.io/oauth/getfetchkey?guid=${guid}`, { headers }).then(r => r.text()),
    fetch(`https://mgmt.aglty.io/oauth/getpreviewkey?guid=${guid}`, { headers }).then(r => r.text()),
  ]);

  return { fetchKey, previewKey };
}
```

> **These are secrets.** The preview key in particular exposes unpublished content. Write them straight into your secret store — never into logs, build output, or anything client-side.

---

## Enum values reference

The Management API's numeric enums — the ones behind `properties.state`, workflow operations, container types — are published at a single endpoint:

```
GET https://mgmt.aglty.io/api/v1/types
```

It needs **no authentication and no instance GUID**, so you can open it in a browser. Each entry gives a `name`, its numeric `value`, and an optional `description`. When a value in this documentation disagrees with that endpoint, the endpoint is right.

The ones you'll reach for most:

**`itemStates`** — what `properties.state` means on a content item or page:

| Name | Value |
| --- | --- |
| `New` | `-1` |
| `None` | `0` |
| `Staging` | `1` |
| `Published` | `2` |
| `Deleted` | `3` |
| `Approved` | `4` |
| `AwaitingApproval` | `5` |
| `Declined` | `6` |
| `Unpublished` | `7` |

**`workflowOperationTypes`** — the `operation` argument for batch workflow calls:

| Name | Value |
| --- | --- |
| `Publish` | `1` |
| `Unpublish` | `2` |
| `Approve` | `3` |
| `Decline` | `4` |
| `RequestApproval` | `5` |

**`contentModelTypes`** — the model type on a container:

| Name | Value |
| --- | --- |
| `Item` | `0` |
| `List` | `1` |
| `Module` | `2` |

**`contentViewTypes`** — the `contentType` filter for paged container listings:

| Name | Value |
| --- | --- |
| `All` | `0` |
| `Shared` | `1` |
| `Linked` | `2` |
| `DynamicPageList` | `3` |

**`batchStates`** — progress of a batch operation:

| Name | Value |
| --- | --- |
| `None` | `0` |
| `Pending` | `1` |
| `InProcess` | `2` |
| `Processed` | `3` |
| `Deleted` | `4` |

The same response also carries `modes`, `pageTypes`, `pageItemTemplateTypes`, `batchItemTypes`, `batchOperationTypes`, `instancePermissions`, `userTypes`, `assetGroupingTypes`, `notificationTypes`, `changeItemTypes`, and `changeTypes`. Rather than hard-coding any of these, read them once at startup:

```ts
const types = await fetch('https://mgmt.aglty.io/api/v1/types').then(r => r.json());

const itemState = Object.fromEntries(
  types.itemStates.map((e: { name: string; value: number }) => [e.name, e.value])
);

console.log(itemState.Published); // 2
```

---

## Error handling

In the .NET SDK, a failed call throws `AgilityManagementException`, which carries the HTTP status code, the API's error message and a request ID.

```csharp
try
{
    var users = await client.InstanceUsers.GetUsersAsync(guid);
}
catch (AgilityManagementException ex)
{
    Console.Error.WriteLine($"Failed ({ex.StatusCode}): {ex.ApiMessage ?? ex.Message}");
}
```

---

Index of every docs page: https://agilitycms.com/docs/llms.txt
