---
title: Add roles to a user
slug: api-documentation/add-roles-to-a-user
docTags: 
createdAt: 2026-08-27T17:06:32.549Z
---

{
  "id": "5sXOjdzmD6vO4oYYx-ykf",
  "type": "api-oas-v2",
  "data": {
    "method": "PUT",
    "url": "https://api.upstartcommerce.com/profile-permissions/users/{userId}/roles",
    "servers": [
      {
        "url": "https://api.upstartcommerce.com/profile-permissions/users/{userId}/roles",
        "description": "Production"
      }
    ],
    "name": "Add roles to a user",
    "description": "<p>Adds the specified roles to the user. This is an additive operation -\nexisting roles are preserved. Adding a role that the user already has\nis idempotent (no error, no duplicate).</p>\n<p><strong>Required Permission:</strong> <code>profile-permissions.users.roles.write</code></p>",
    "contentType": "application/json",
    "request": {
      "pathParameters": [
        {
          "kind": "required",
          "name": "userId",
          "type": "string<uuid>",
          "description": "User identifier",
          "format": "uuid"
        }
      ],
      "headerParameters": [
        {
          "name": "accept",
          "type": "string",
          "kind": "optional",
          "description": "Generated from available response content types",
          "enum": [
            "application/json"
          ],
          "default": "application/json"
        }
      ],
      "queryParameters": [],
      "bodyDataParameters": [
        {
          "kind": "required",
          "name": "body",
          "type": "object",
          "description": "The roles to assign to this user.",
          "customType": "AddUserRolesRequest",
          "schema": [
            {
              "name": "roleIds",
              "kind": "required",
              "type": "array",
              "description": "Role IDs to add to the user",
              "example": "[\"role-operator\",\"role-viewer\"]",
              "itemType": "string",
              "customType": "string[]",
              "itemExample": "",
              "itemDefault": ""
            },
            {
              "name": "expiresAt",
              "kind": "optional",
              "type": "string<date-time>",
              "description": "Optional expiration for these role assignments",
              "format": "date-time"
            }
          ],
          "modelRef": "#/components/schemas/AddUserRolesRequest",
          "isExpanded": true
        }
      ],
      "formDataParameters": [],
      "oAuthParameters": [
        {
          "id": "bearerAuth",
          "name": "bearerAuth",
          "kind": "optional",
          "type": "http",
          "description": "<p>JWT token from authentication service.</p>\n<p>The token contains:</p>\n<ul>\n<li>sub: User ID</li>\n<li>tenant: Tenant ID (used for all operations)</li>\n<li>roles: Array of role IDs</li>\n<li>type: \"management\" or \"consumer\"</li>\n</ul>\n<p>Alternatively, tenant can be specified via X-Tenant-Id header.</p>",
          "scheme": "bearer"
        }
      ],
      "cookieParameters": []
    },
    "responses": [
      {
        "statusCode": "200",
        "description": "Roles added successfully",
        "jsonExample": "",
        "isExpanded": true,
        "schema": [
          {
            "kind": "optional",
            "type": "object",
            "description": "<p>Every role assigned to a user, with its assignment metadata.</p>",
            "customType": "UserRolesResponse",
            "schema": [
              {
                "name": "userId",
                "kind": "required",
                "type": "string<uuid>",
                "description": "",
                "format": "uuid"
              },
              {
                "name": "roles",
                "kind": "required",
                "type": "array",
                "description": "<p>One role assigned to a user, recording who assigned it, when, and when it expires.</p>",
                "modelRef": "#/components/schemas/UserRoleAssignment",
                "customType": "UserRoleAssignment[]",
                "schema": [
                  {
                    "name": "roleId",
                    "kind": "required",
                    "type": "string",
                    "description": "Role identifier"
                  },
                  {
                    "name": "roleName",
                    "kind": "required",
                    "type": "string",
                    "description": "Role name",
                    "example": "operator"
                  },
                  {
                    "name": "assignedAt",
                    "kind": "required",
                    "type": "string<date-time>",
                    "description": "When the role was assigned",
                    "format": "date-time"
                  },
                  {
                    "name": "assignedBy",
                    "kind": "optional",
                    "type": "string",
                    "description": "<p>User ID who assigned this role (null if system-assigned)</p>"
                  },
                  {
                    "name": "expiresAt",
                    "kind": "optional",
                    "type": "string<date-time>",
                    "description": "<p>When the role assignment expires (null if permanent)</p>",
                    "format": "date-time"
                  }
                ]
              }
            ],
            "modelRef": "#/components/schemas/UserRolesResponse",
            "isExpanded": true
          }
        ]
      },
      {
        "statusCode": "400",
        "description": "Invalid request parameters",
        "jsonExample": "",
        "isExpanded": true,
        "schema": [
          {
            "kind": "optional",
            "type": "object",
            "description": "<p>Error payload identifying the failure by error code and message, with optional detail.</p>",
            "customType": "ErrorResponse",
            "schema": [
              {
                "name": "error",
                "kind": "required",
                "type": "string<bad_request | unauthorized | forbidden | not_found | conflict | internal_error>",
                "description": "Error code",
                "example": "not_found",
                "enum": [
                  "bad_request",
                  "unauthorized",
                  "forbidden",
                  "not_found",
                  "conflict",
                  "internal_error"
                ]
              },
              {
                "name": "message",
                "kind": "required",
                "type": "string",
                "description": "<p>Human-readable error message</p>",
                "example": "Role not found"
              },
              {
                "name": "details",
                "kind": "optional",
                "type": "map",
                "description": "Additional error details",
                "example": "",
                "customType": "map<string, string>",
                "schema": []
              }
            ],
            "modelRef": "#/components/schemas/ErrorResponse",
            "isExpanded": true
          }
        ]
      },
      {
        "statusCode": "401",
        "description": "Missing or invalid authentication token",
        "jsonExample": "",
        "isExpanded": true,
        "schema": [
          {
            "kind": "optional",
            "type": "object",
            "description": "<p>Error payload identifying the failure by error code and message, with optional detail.</p>",
            "customType": "ErrorResponse",
            "schema": [
              {
                "name": "error",
                "kind": "required",
                "type": "string<bad_request | unauthorized | forbidden | not_found | conflict | internal_error>",
                "description": "Error code",
                "example": "not_found",
                "enum": [
                  "bad_request",
                  "unauthorized",
                  "forbidden",
                  "not_found",
                  "conflict",
                  "internal_error"
                ]
              },
              {
                "name": "message",
                "kind": "required",
                "type": "string",
                "description": "<p>Human-readable error message</p>",
                "example": "Role not found"
              },
              {
                "name": "details",
                "kind": "optional",
                "type": "map",
                "description": "Additional error details",
                "example": "",
                "customType": "map<string, string>",
                "schema": []
              }
            ],
            "modelRef": "#/components/schemas/ErrorResponse",
            "isExpanded": true
          }
        ]
      },
      {
        "statusCode": "404",
        "description": "User or role not found",
        "jsonExample": "",
        "isExpanded": true,
        "schema": [
          {
            "kind": "optional",
            "type": "object",
            "description": "<p>Error payload identifying the failure by error code and message, with optional detail.</p>",
            "customType": "ErrorResponse",
            "schema": [
              {
                "name": "error",
                "kind": "required",
                "type": "string<bad_request | unauthorized | forbidden | not_found | conflict | internal_error>",
                "description": "Error code",
                "example": "not_found",
                "enum": [
                  "bad_request",
                  "unauthorized",
                  "forbidden",
                  "not_found",
                  "conflict",
                  "internal_error"
                ]
              },
              {
                "name": "message",
                "kind": "required",
                "type": "string",
                "description": "<p>Human-readable error message</p>",
                "example": "Role not found"
              },
              {
                "name": "details",
                "kind": "optional",
                "type": "map",
                "description": "Additional error details",
                "example": "",
                "customType": "map<string, string>",
                "schema": []
              }
            ],
            "modelRef": "#/components/schemas/ErrorResponse",
            "isExpanded": true
          }
        ]
      },
      {
        "statusCode": "500",
        "description": "Internal server error",
        "jsonExample": "",
        "isExpanded": true,
        "schema": [
          {
            "kind": "optional",
            "type": "object",
            "description": "<p>Error payload identifying the failure by error code and message, with optional detail.</p>",
            "customType": "ErrorResponse",
            "schema": [
              {
                "name": "error",
                "kind": "required",
                "type": "string<bad_request | unauthorized | forbidden | not_found | conflict | internal_error>",
                "description": "Error code",
                "example": "not_found",
                "enum": [
                  "bad_request",
                  "unauthorized",
                  "forbidden",
                  "not_found",
                  "conflict",
                  "internal_error"
                ]
              },
              {
                "name": "message",
                "kind": "required",
                "type": "string",
                "description": "<p>Human-readable error message</p>",
                "example": "Role not found"
              },
              {
                "name": "details",
                "kind": "optional",
                "type": "map",
                "description": "Additional error details",
                "example": "",
                "customType": "map<string, string>",
                "schema": []
              }
            ],
            "modelRef": "#/components/schemas/ErrorResponse",
            "isExpanded": true
          }
        ]
      }
    ],
    "hasXCodeSamples": false,
    "examples": {
      "languages": [
        {
          "id": "wqG6XUweE4jKZEfjUtdWI",
          "language": "curl",
          "label": "cURL",
          "code": "curl --request PUT \\\n     --url https://api.upstartcommerce.com/profile-permissions/users/{userId}/roles \\\n     --header 'accept: application/json' \\\n     --header 'content-type: application/json' \\\n     --data '{\n     \"roleIds\": []\n     }'"
        },
        {
          "id": "23glMUoNKrX5OJf1ZJ9ip",
          "language": "javascript",
          "label": "javascript",
          "code": "var myHeaders = new Headers();\nmyHeaders.append(\"accept\", \"application/json\");\nmyHeaders.append(\"content-type\", \"application/json\");\n\nvar raw = JSON.stringify({\n   \"roleIds\": []\n});\n\nvar requestOptions = {\n   method: 'PUT',\n   headers: myHeaders,\n   body: raw,\n   redirect: 'follow'\n};\n\nfetch(\"https://api.upstartcommerce.com/profile-permissions/users/{userId}/roles\", requestOptions)\n   .then(response => response.text())\n   .then(result => console.log(result))\n   .catch(error => console.log('error', error));"
        },
        {
          "id": "UNJH3y2D_DnnOkLLKNfDz",
          "language": "ruby",
          "label": "Ruby",
          "code": "require \"uri\"\nrequire \"json\"\nrequire \"net/http\"\n\nurl = URI(\"https://api.upstartcommerce.com/profile-permissions/users/{userId}/roles\")\n\nhttps = Net::HTTP.new(url.host, url.port)\nhttps.use_ssl = true\n\nrequest = Net::HTTP::Put.new(url)\nrequest[\"accept\"] = \"application/json\"\nrequest[\"content-type\"] = \"application/json\"\nrequest.body = JSON.dump({\n   \"roleIds\": []\n})\n\nresponse = https.request(request)\nputs response.read_body\n"
        },
        {
          "id": "hk7ycWO9JnGo-ZcjSgfsk",
          "language": "python",
          "label": "Python",
          "code": "import requests\nimport json\n\nurl = \"https://api.upstartcommerce.com/profile-permissions/users/{userId}/roles\"\n\npayload = json.dumps({\n   \"roleIds\": []\n})\nheaders = {\n   'accept': 'application/json',\n   'content-type': 'application/json'\n}\n\nresponse = requests.request(\"PUT\", url, headers=headers, data=payload)\n\nprint(response.text)\n"
        }
      ],
      "selectedLanguageId": "wqG6XUweE4jKZEfjUtdWI"
    },
    "results": {
      "languages": [
        {
          "id": "19452FNhpZfpbsnNWlICi",
          "language": "200",
          "code": "// Roles added successfully\n{\n  \"userId\": \"\",\n  \"roles\": [\n    {\n      \"roleId\": \"\",\n      \"roleName\": \"operator\",\n      \"assignedAt\": \"\",\n      \"assignedBy\": \"\",\n      \"expiresAt\": \"\"\n    }\n  ]\n}"
        },
        {
          "id": "6HGzZ0kd4JXoIFc5Ds5XK",
          "language": "400",
          "code": "// Invalid request parameters\n{\n  \"error\": \"not_found\",\n  \"message\": \"Role not found\",\n  \"details\": \"\"\n}"
        },
        {
          "id": "WakFrtfdbtloPvsLEjZlh",
          "language": "401",
          "code": "// Missing or invalid authentication token\n{\n  \"error\": \"not_found\",\n  \"message\": \"Role not found\",\n  \"details\": \"\"\n}"
        },
        {
          "id": "PSlGO7udNYieIzjG4-EHp",
          "language": "404",
          "code": "// User or role not found\n{\n  \"error\": \"not_found\",\n  \"message\": \"Role not found\",\n  \"details\": \"\"\n}"
        },
        {
          "id": "5ZpQlFJnTrSgdf3nMAtZb",
          "language": "500",
          "code": "// Internal server error\n{\n  \"error\": \"not_found\",\n  \"message\": \"Role not found\",\n  \"details\": \"\"\n}"
        }
      ],
      "selectedLanguageId": "19452FNhpZfpbsnNWlICi"
    }
  },
  "children": [
    {
      "text": ""
    }
  ]
}