{
  "id": "environment-2",
  "title": "Organization and Service Accounts: Identify the Organization, Create a Service Account, Issue and Revoke Its Credentials",
  "description": "Manage the organization layer of the Config API: read the current organization, create a Service Account with the all_editor or all_viewer role, issue a credential and keep its file, list and inspect credentials, update the account with an ETag, and revoke a credential.",
  "category": "Environment",
  "tags": [
    "Environment Setup",
    "Organization",
    "Service Account",
    "Credentials",
    "Revocation",
    "ETag"
  ],
  "api": "Environment",
  "language": "Json",
  "lastUpdated": "2026-10-12",
  "openApiEndpoints": [
    "/configs/v1/organizations/current",
    "/configs/v1/service-accounts",
    "/configs/v1/service-accounts/{id}",
    "/configs/v1/service-account-credentials",
    "/configs/v1/service-account-credentials/{id}"
  ],
  "relatedGuides": [
    "/guides/guide-environment",
    "/guides/guide-credentials",
    "/guides/guide-terraform",
    "/terraform/terraform-14"
  ],
  "commonErrors": [
    {
      "code": "401",
      "message": "Invalid ServiceAccount JWT in Authorization header",
      "solution": "The Bearer token is missing, malformed, expired, or belongs to a revoked credential. An Application Agent credential (X-IK-ClientKey) is not accepted on the Config API."
    },
    {
      "code": "403",
      "message": "Forbidden",
      "solution": "The calling Service Account has the all_viewer role; create, update and delete need all_editor."
    },
    {
      "code": "409",
      "message": "Conflict",
      "solution": "A Service Account with that name already exists in the organization. name is immutable, so pick another one."
    },
    {
      "code": "412",
      "message": "Precondition Failed",
      "solution": "The If-Match value is not the object's current etag: read it again and retry with the new etag."
    },
    {
      "code": "415",
      "message": "Content-Type must be application/json",
      "solution": "Send the body as JSON with Content-Type: application/json."
    },
    {
      "code": "422",
      "message": "invalid field name: is not valid name, value must start with [a-z] followed by [-a-z0-9] and ends with [a-z0-9]",
      "solution": "name is 2 to 63 characters of lowercase letters, digits and hyphens, starting with a letter and ending with a letter or digit."
    },
    {
      "code": "422",
      "message": "invalid field role: \"root\" does not meet rule oneof=all_editor all_viewer",
      "solution": "role is all_editor or all_viewer."
    },
    {
      "code": "422",
      "message": "invalid field organization_id: identifier is not of ORGANIZATION",
      "solution": "organization_id must be the organization GID from GET /configs/v1/organizations/current, not a project or account GID."
    },
    {
      "code": "422",
      "message": "invalid field expire_time: \"tomorrow\" does not meet rule datetime=2006-01-02T15:04:05Z07:00",
      "solution": "expire_time is an RFC 3339 timestamp in the future. A value more than two years ahead is accepted, but the credentials file then carries no token."
    },
    {
      "code": "422",
      "message": "nothing to change",
      "solution": "A PUT must set display_name and/or description; null leaves a field unchanged, an empty string clears it."
    }
  ],
  "summary": "Everything above a project lives in the organization: the Service Accounts that call the Config API and run Terraform, and their credentials. This example covers that layer end to end with the Config API:\n1. Read the organization the calling Service Account belongs to (GET /configs/v1/organizations/current).\n2. Create a Service Account with a role: all_editor may change configuration, all_viewer may only read.\n3. Issue a credential for it; the private key comes back once, in service_account_config.\n4. List and read credentials: metadata only, never the key again.\n5. Update the account's display_name or description with the ETag it was read with.\n6. Revoke a credential, then delete the account.\nEvery call carries a Service Account Bearer token; an Application Agent credential (X-IK-ClientKey) is refused.",
  "usecase": "Scenario: a CI pipeline needs its own identity to apply the organization's Terraform modules, separate from the people's accounts, and the security team wants credentials rotated and revocable without touching the Hub.\n- An administrator's Service Account creates the pipeline's account and its first credential and hands the credential file to the pipeline's secret store.\n- Rotation: issue a second credential, switch the pipeline, then delete the first. A deleted credential's token is refused at once with 401; the account's other credentials keep working.\n- A read-only auditor gets the same account shape with role all_viewer.\nThe same resources exist in Terraform: see the Service account example in Terraform Configurations.",
  "requirements": "Prerequisites:\n- ServiceAccount credentials with the all_editor role, as a Bearer token in the Authorization header. An all_viewer account can read and list but gets 403 on create, update and delete.\n- The organization's region: https://eu.api.indykite.com or https://us.api.indykite.com.\n\nRequired API access:\n- GET /configs/v1/organizations/current\n- POST, GET, PUT, DELETE /configs/v1/service-accounts\n- POST, GET, DELETE /configs/v1/service-account-credentials",
  "steps": "Step 1: Read the current organization\n- GET /configs/v1/organizations/current, no parameters.\n- Result: the organization's id, name, display_name, description and audit fields, with an etag header. The id is the organization_id every other call needs.\n\nStep 2: Create a Service Account\n- POST /configs/v1/service-accounts with organization_id, name, role, and optionally display_name and description.\n- name is immutable: 2 to 63 characters, lowercase letters, digits and hyphens, starting with a letter and ending with a letter or digit, unique in the organization.\n- role is all_editor or all_viewer; it cannot be changed later.\n- Result: 201 with the account's id and an etag header. Read it back by id or by name: the response carries name, display_name, description and organization_id, not the role.\n\nStep 3: Issue a credential\n- POST /configs/v1/service-account-credentials with service_account_id, and optionally display_name (2 to 254 characters) and expire_time (RFC 3339, in the future).\n- Result: 201 with id, kid, expire_time and the credentials file in service_account_config. With an expire_time the file carries a ready-to-use token; without one the credential never expires and the file carries only the private key, with which you sign your own JWT (Credentials guide).\n- Keep service_account_config: the private key is not returned again.\n\nStep 4: List and read credentials\n- GET /configs/v1/service-account-credentials?organization_id=<gid>, optionally &search=<text> on name, display name or description; GET /configs/v1/service-account-credentials/{id} for one.\n- Result: metadata only: id, kid, display_name, service_account_id, organization_id, expire_time, create_time, created_by.\n\nStep 5: Update the account\n- PUT /configs/v1/service-accounts/{id} with display_name and/or description; a field left null is unchanged, an empty string clears it, an empty body is refused with 422 \"nothing to change\".\n- Send the current etag as If-Match to refuse a write over a concurrent change: a stale value gets 412 Precondition Failed. If-Match is optional.\n- Result: 200 with the audit fields and a new etag.\n\nStep 6: Revoke and delete\n- DELETE /configs/v1/service-account-credentials/{id}: the credential's token is refused from then on; other credentials of the account are untouched.\n- DELETE /configs/v1/service-accounts/{id}, optionally with If-Match, once nothing uses the account. Both return the deleted id.\nThese organization-level calls produce indykite.audit.config.* events (creating an account also records the role grant as indykite.audit.config.permission.assign), but such events belong to no project: they are neither delivered to an Event Sink nor chained in a project's tamper-proof trail.",
  "code": [
    {
      "step": "Step 1",
      "language": "Bash",
      "code": "export API_URL=https://eu.api.indykite.com   # or https://us.api.indykite.com, the region of your organization\nexport SA_TOKEN=\"<token from the credentials file of an existing all_editor Service Account>\""
    },
    {
      "step": "Step 1",
      "description": "Read the organization of the calling Service Account. No parameters; the id in the response is the organization_id for every call below.",
      "filename": "GET https://eu.api.indykite.com/configs/v1/organizations/current",
      "language": "Bash",
      "code": "curl -sS \"$API_URL/configs/v1/organizations/current\" \\\n  -H \"Authorization: Bearer $SA_TOKEN\" -D - -o organization.json\nexport ORG_ID=$(jq -r .id organization.json)   # the organization GID, needed by every call below"
    },
    {
      "description": "The 200 response, with an etag header alongside it.",
      "language": "Json",
      "code": "\n{\n  \"id\": \"gid:YOUR_ORGANIZATION_GID\",\n  \"name\": \"your-organization-name\",\n  \"display_name\": \"Your organization\",\n  \"description\": \"\",\n  \"create_time\": \"2025-03-04T10:12:33Z\",\n  \"created_by\": \"gid:YOUR_SERVICE_ACCOUNT_GID\",\n  \"update_time\": \"2025-03-04T10:12:33Z\",\n  \"updated_by\": \"gid:YOUR_SERVICE_ACCOUNT_GID\"\n}"
    },
    {
      "step": "Step 2",
      "description": "Create the Service Account. name is immutable and must be unique in the organization; role is all_editor or all_viewer and cannot be changed afterwards. A duplicate name is refused with 409, a bad name or role with 422 and the exact rule in errors[].",
      "filename": "POST https://eu.api.indykite.com/configs/v1/service-accounts",
      "language": "Json",
      "ingest": {
        "organization_id": "gid:YOUR_ORGANIZATION_GID",
        "name": "terraform-ci",
        "display_name": "CI pipeline",
        "description": "Applies this organization's Terraform modules",
        "role": "all_editor"
      }
    },
    {
      "description": "The 201 response: the new account's id and audit fields, plus the etag header.",
      "language": "Json",
      "code": "\nHTTP/2 201\netag: 2v3tVyPcKq4\n\n{\n  \"id\": \"gid:YOUR_SERVICE_ACCOUNT_GID\",\n  \"create_time\": \"2026-10-12T14:02:11Z\",\n  \"created_by\": \"gid:YOUR_ADMIN_SERVICE_ACCOUNT_GID\",\n  \"update_time\": \"2026-10-12T14:02:11Z\",\n  \"updated_by\": \"gid:YOUR_ADMIN_SERVICE_ACCOUNT_GID\"\n}"
    },
    {
      "step": "Step 2",
      "description": "Read the account back, by GID or by name. The response carries the fields you set except the role; ?version=<n> reads an earlier version.",
      "filename": "GET https://eu.api.indykite.com/configs/v1/service-accounts/{id}",
      "language": "Bash",
      "code": "# by GID, or by name within the calling account's organization\ncurl -sS \"$API_URL/configs/v1/service-accounts/terraform-ci\" \\\n  -H \"Authorization: Bearer $SA_TOKEN\" -D - | sed -n '/^etag/p;/^{/,$p'"
    },
    {
      "language": "Json",
      "code": "\n{\n  \"id\": \"gid:YOUR_SERVICE_ACCOUNT_GID\",\n  \"name\": \"terraform-ci\",\n  \"display_name\": \"CI pipeline\",\n  \"description\": \"Applies this organization's Terraform modules\",\n  \"organization_id\": \"gid:YOUR_ORGANIZATION_GID\",\n  \"create_time\": \"2026-10-12T14:02:11Z\",\n  \"created_by\": \"gid:YOUR_ADMIN_SERVICE_ACCOUNT_GID\",\n  \"update_time\": \"2026-10-12T14:02:11Z\",\n  \"updated_by\": \"gid:YOUR_ADMIN_SERVICE_ACCOUNT_GID\"\n}"
    },
    {
      "step": "Step 3",
      "description": "Issue a credential. expire_time is optional: set within two years it yields a ready-to-use token in the file; farther ahead, or omitted (the credential then never expires), the file carries only the private key. A past date or a malformed one is refused with 422.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/service-account-credentials",
      "language": "Json",
      "ingest": {
        "service_account_id": "gid:YOUR_SERVICE_ACCOUNT_GID",
        "display_name": "CI pipeline credential",
        "expire_time": "2027-10-01T00:00:00Z"
      }
    },
    {
      "description": "The 201 response. service_account_config is the credentials file, the only time the private key is returned; kid names the public key the platform keeps.",
      "language": "Json",
      "code": "\nHTTP/2 201\n\n{\n  \"id\": \"gid:YOUR_SERVICE_ACCOUNT_CREDENTIAL_GID\",\n  \"kid\": \"…\",\n  \"display_name\": \"CI pipeline credential\",\n  \"service_account_id\": \"gid:YOUR_SERVICE_ACCOUNT_GID\",\n  \"expire_time\": \"2027-10-01T00:00:00Z\",\n  \"create_time\": \"2026-10-12T14:05:40Z\",\n  \"created_by\": \"gid:YOUR_ADMIN_SERVICE_ACCOUNT_GID\",\n  \"service_account_config\": {\n    \"serviceAccountId\": \"gid:YOUR_SERVICE_ACCOUNT_GID\",\n    \"endpoint\": \"eu.api.indykite.com\",\n    \"token\": \"eyJhbGciOiJFUzI1NiIsImtpZCI6Iu2026…\",\n    \"privateKeyJWK\": {\n      \"kty\": \"EC\",\n      \"crv\": \"P-256\",\n      \"alg\": \"ES256\",\n      \"use\": \"sig\",\n      \"kid\": \"…\",\n      \"x\": \"…\",\n      \"y\": \"…\",\n      \"d\": \"…\"\n    },\n    \"privateKeyPKCS8\": \"<the same key as PEM, PKCS #8>\",\n    \"privateKeyPKCS8Base64\": \"<the PEM, Base64-encoded>\"\n  }\n}"
    },
    {
      "step": "Step 3",
      "description": "Keep the file straight from the response into the pipeline's secret store (here a local file for illustration).",
      "language": "Bash",
      "code": "# keep only the file your pipeline needs; the private key is never returned again\ncurl -sS -X POST \"$API_URL/configs/v1/service-account-credentials\" \\\n  -H \"Authorization: Bearer $SA_TOKEN\" -H \"Content-Type: application/json\" \\\n  --data @credential-create.json | jq .service_account_config > terraform-ci-credentials.json\nchmod 600 terraform-ci-credentials.json"
    },
    {
      "step": "Step 4",
      "description": "List the organization's credentials, optionally narrowed with search. Metadata only.",
      "filename": "GET https://eu.api.indykite.com/configs/v1/service-account-credentials?organization_id=<gid>",
      "language": "Bash",
      "code": "curl -sS \"$API_URL/configs/v1/service-account-credentials?organization_id=$ORG_ID&search=CI\" \\\n  -H \"Authorization: Bearer $SA_TOKEN\""
    },
    {
      "language": "Json",
      "code": "\n{\n  \"data\": [\n    {\n      \"id\": \"gid:YOUR_SERVICE_ACCOUNT_CREDENTIAL_GID\",\n      \"kid\": \"…\",\n      \"display_name\": \"CI pipeline credential\",\n      \"organization_id\": \"gid:YOUR_ORGANIZATION_GID\",\n      \"service_account_id\": \"gid:YOUR_SERVICE_ACCOUNT_GID\",\n      \"expire_time\": \"2027-10-01T00:00:00Z\",\n      \"create_time\": \"2026-10-12T14:05:40Z\",\n      \"created_by\": \"gid:YOUR_ADMIN_SERVICE_ACCOUNT_GID\"\n    }\n  ]\n}"
    },
    {
      "step": "Step 5",
      "description": "Update display_name and clear description, guarded by the etag read just before. A stale If-Match is refused with 412; a body with nothing to change with 422.",
      "filename": "PUT https://eu.api.indykite.com/configs/v1/service-accounts/{id}",
      "language": "Bash",
      "code": "# read first to get the current etag, then send it back as If-Match\nETAG=$(curl -sS \"$API_URL/configs/v1/service-accounts/gid:YOUR_SERVICE_ACCOUNT_GID\" \\\n  -H \"Authorization: Bearer $SA_TOKEN\" -D - -o /dev/null | awk 'tolower($1)==\"etag:\" {print $2}' | tr -d '\\r')\ncurl -sS -X PUT \"$API_URL/configs/v1/service-accounts/gid:YOUR_SERVICE_ACCOUNT_GID\" \\\n  -H \"Authorization: Bearer $SA_TOKEN\" -H \"Content-Type: application/json\" -H \"If-Match: $ETAG\" \\\n  --data '{\"display_name\": \"CI pipeline (eu)\", \"description\": \"\"}'"
    },
    {
      "language": "Json",
      "code": "\n{\n  \"id\": \"gid:YOUR_SERVICE_ACCOUNT_GID\",\n  \"create_time\": \"2026-10-12T14:02:11Z\",\n  \"created_by\": \"gid:YOUR_ADMIN_SERVICE_ACCOUNT_GID\",\n  \"update_time\": \"2026-10-12T15:20:07Z\",\n  \"updated_by\": \"gid:YOUR_ADMIN_SERVICE_ACCOUNT_GID\"\n}"
    },
    {
      "step": "Step 6",
      "description": "Revoke one credential, then delete the account once nothing depends on it. Both return the deleted id; a revoked credential's token gets 401 immediately.",
      "filename": "DELETE https://eu.api.indykite.com/configs/v1/service-account-credentials/{id}",
      "language": "Bash",
      "code": "# revoke one credential; the account's other credentials keep working\ncurl -sS -X DELETE \"$API_URL/configs/v1/service-account-credentials/gid:YOUR_SERVICE_ACCOUNT_CREDENTIAL_GID\" \\\n  -H \"Authorization: Bearer $SA_TOKEN\"\n# {\"id\": \"gid:YOUR_SERVICE_ACCOUNT_CREDENTIAL_GID\"}\n\n# remove the account itself once nothing uses it (If-Match is optional here too)\ncurl -sS -X DELETE \"$API_URL/configs/v1/service-accounts/gid:YOUR_SERVICE_ACCOUNT_GID\" \\\n  -H \"Authorization: Bearer $SA_TOKEN\"\n# {\"id\": \"gid:YOUR_SERVICE_ACCOUNT_GID\"}"
    }
  ],
  "links": {
    "html": "/resources/environment-2",
    "markdown": "/resources/environment-2.md",
    "openApi": "https://openapi.indykite.com/"
  }
}