TABLE OF CONTENTS
ADMIN PRIVILEGES REQUIRED
Overview
The introduction of Private Teams in Stack Internal Community allows site administrators to restrict the sharing of sensitive information to only those who should have access. To streamline and automate management of Private Team rosters, Stack Internal Community offers team membership sync via an API push.
API v2.3 is being deprecated. Private Teams membership sync has no API v3 endpoint; continue using the versionless /api/enterprise/usersync endpoint described here. This endpoint will remain available after API v2.3 is disabled. See the Migrate from API v2 to API v3 article for migration guidance.
Syncing team membership via API push
To enable API team membership sync:
- Click Admin settings in the left-hand menu, then Private Teams sync.
- Click Push data via API.
- Click Use API push.
- In Admin settings > Private Teams, open each Private Team you want to sync and turn API sync On. Teams remain manually managed until you enable syncing for them.
With team membership sync enabled for a Private Team, your site updates its membership each time you send a properly formatted request to https://[your_site].stackenterprise.co/api/enterprise/usersync. This is your site's user sync API endpoint.
This special endpoint is not listed in your site's interactive API documentation. Its request and response formats are described below.
Team membership sync authorization
To use the API to sync team memberships, create a read-write user API application owned by a site administrator and obtain an OAuth access token for that application. The sync endpoint rejects applications owned by regular users and Community service applications. See Secure API Token Generation with OAuth and PKCE for obtaining a token. Use a token with the write_access scope.
This endpoint requires both the application key and its associated OAuth access token. Send them in the X-API-Key and X-API-Access-Token headers. An Authorization: Bearer header alone does not authenticate this endpoint. Keep the key and token out of the URL.
To call the user sync API, make an HTTPS POST request encoded as application/x-www-form-urlencoded to https://[your_site].stackenterprise.co/api/enterprise/usersync. Use the following headers and form fields.
| Header or form field | Definition |
|---|---|
X-API-Key (header) |
The application key from the administrator-owned, read-write API application. |
X-API-Access-Token (header) |
An OAuth access token associated with that application. |
requestsJson (form field) |
A JSON array of user sync requests, with one entry for each team. Percent-encode the JSON value as part of the application/x-www-form-urlencoded form body. cURL's --data-urlencode does this for you; do not percent-encode the value first when using that option. |
dryRun (form field) |
Set to true to preview account resolution and intended changes without changing membership. Set to false or omit it to apply the changes. |
The access_token can also be sent as a form field. The key can be sent as a form field only when Admin settings > Security > API access > Force use of headers for API keys is off. When that setting is on, omit the key form field and send the key only in the X-API-Key header. The header example below works with either setting.
Example JSON request
[
{
"Team": "productone",
"Members": [
{ "UserIdentifier": "alice@example.com", "Level": "Admin" },
{ "UserIdentifier": "bob@example.com", "Level": "Member" }
]
}
]
Include the following fields in each JSON request:
- Team The Private Team's URL slug (for example,
productonein/c/productone). - UserIdentifier The user's credential string, such as a SAML NameID or Active Directory SID, rather than their numeric Stack Internal account or Team user ID. The sync process also tries a matching account email address when it cannot resolve the authentication identifier.
- Level The user's role in the Private Team:
Adminfor a team owner orMemberfor a regular member.Moderatoris not a supported value.
The team sync API call is a full update of each team's membership roster, not an append operation. Include all members of each team in every request for that team. A real run removes existing team members omitted from the request. Run a dry run and check the intended changes before applying a new roster.
Example HTTP request
This cURL command percent-encodes the JSON above into the requestsJson form field and previews the result. Replace the site, key, token, team slug, and user identifiers with your values. To apply the roster after reviewing the dry run, change dryRun=true to dryRun=false.
curl --request POST 'https://[your_site].stackenterprise.co/api/enterprise/usersync' \
--header 'X-API-Key: YOUR_APPLICATION_KEY' \
--header 'X-API-Access-Token: YOUR_ADMIN_OAUTH_ACCESS_TOKEN' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'User-Agent: team-membership-sync/1.0' \
--data-urlencode 'requestsJson=[{"Team":"productone","Members":[{"UserIdentifier":"alice@example.com","Level":"Admin"},{"UserIdentifier":"bob@example.com","Level":"Member"}]}]' \
--data-urlencode 'dryRun=true'
Example HTTP response
The following abbreviated example shows the response shape for a successful dry run. Account IDs, intended changes, and the human-readable log depend on the current roster.
{
"items": [
{
"HasErrors": false,
"Results": [
{
"Team": "productone",
"StatusCode": "SuccessfulDryRun",
"SyncResult": {
"Status": "SuccessfulDryRun",
"SiteName": "productone",
"IntendedChanges": [
{
"AccountId": 12345,
"SiteUserId": 678,
"CurrentUserType": "Registered",
"NewUserType": "Admin",
"IsDeactivated": false,
"Change": "ChangeUserType"
}
],
"Log": "START User Sync from Push for Team productone\nDesired Site Membership (Total Count: 2):\nLooking up Accounts...\nDetermining intended changes...\nThis is only a dry-run - not actually performing changes.\nFinal Sync Result: SuccessfulDryRun\nEND User Sync"
}
}
]
}
],
"has_more": false,
"page": 1,
"page_size": 1,
"total": 1,
"type": "user_sync_api_response"
}
Status codes—overall sync process
The HTTP response includes a StatusCode for each Private Team requested. Check HasErrors and each team's StatusCode; an HTTP success alone does not mean that every team synced successfully.
| Status code | Definition |
|---|---|
| Success | The entire sync process succeeded for this team. |
| PartialSyncFailure | The sync process started, achieved partial success, then failed for this team. |
| SuccessfulDryRun | The preview completed without applying changes. |
| OtherError | An unspecified error occurred. |
| UserSyncNotEnabled | API sync is not enabled for this Private Team. |
| SyncSourceMustBePush | The site is not configured to receive membership data through API push. |
| ErrorTryingToResolveUsers | A system error occurred while resolving user identifiers. |
| FailedToDetermineChanges | A system error occurred while determining intended changes. |
| ErrorApplyingChanges | A system error occurred while applying changes. |
| TeamNotFound | No Private Team with the given slug could be found. |
The endpoint returns a single page with one items entry for the batch. For example, a request for a team that does not exist returns:
{
"items": [{ "HasErrors": true, "Results": [{ "Team": "notexisting", "StatusCode": "TeamNotFound" }] }],
"has_more": false,
"page": 1,
"page_size": 1,
"total": 1,
"type": "user_sync_api_response"
}
The single data item is a root object with two properties:
Resultsis an array containing one result per team inrequestsJson.HasErrorsistrueif any result has an error. Check the individual results to find which team needs attention.
{
"HasErrors": true,
"Results": [{ "Team": "notexisting", "StatusCode": "TeamNotFound" }]
}
Each result item contains information about a single Private Team:
Teamis the URL slug of the team (https://[your_site].stackenterprise.co/c/[team_name]).StatusCodereports the outcome for that team.SyncResultcontains details about the team sync when available.
SyncResult values
| SyncResult value | Definition |
|---|---|
| Status | Same as StatusCode on the result object. |
| SiteName | The display name of the Private Team. |
| IntendedChanges and ActualChanges |
The changes the sync process determined should happen, and the changes it actually applied. ActualChanges is omitted on a dry run. |
| Change | May return NoChange, AddToSite, RemoveFromSite, or ChangeUserType. |
| CurrentUserType and NewUserType |
Admin for team owners or Registered for regular members. These describe the Team's user roles, rather than the request's Admin and Member values. |
| AccountId | The user's account ID. |
| SiteUserId | The user's ID within that Private Team, when present. |
| IsDeactivated | Boolean indicating whether the account is currently deactivated. |
| Log | Human-readable details of the sync operation. Its format may change and is not meant to be machine parsed. |
SyncResult may be absent for some status codes.
Log example
A dry-run log contains the resolved users, intended changes, and a line confirming that it did not apply them. For example:
START User Sync from Push for Team productone
Desired Site Membership (Total Count: 2):
Looking up Accounts...
Determining intended changes...
This is only a dry-run - not actually performing changes.
Final Sync Result: SuccessfulDryRun
END User Sync