Navigation

Create a Database User

Note

Groups and projects are synonymous terms. Your {GROUP-ID} is the same as your project ID. For existing groups, your group/project ID remains the same. The resource and corresponding endpoints use the term groups.

Base URL: https://cloud.mongodb.com/api/atlas/v1.0

The Atlas API uses HTTP Digest Authentication. Provide your Atlas username as the username and Atlas API key as the password as part of the HTTP request.

This endpoint requires that the Atlas user has the Owner role. To view the available Atlas users, click on Users & Teams in the left-hand navigation.

For complete documentation on configuring API access for an Atlas project, see Configure Atlas API Access.

Atlas supports a maximum of 100 database users per Atlas project.

Note

If you require more than 100 database users on a project, please contact Atlas support.

Syntax

POST /api/atlas/v1.0/groups/{GROUP-ID}/databaseUsers

Request Path Parameters

Path Parameter Type Necessity Description
GROUP-ID string Required Unique identifier for the project.

Request Query Parameters

This endpoint may use any of the HTTP request query parameters available to all Atlas API resources. These are all optional.

Name Type Necessity Description Default
pageNum integer Optional Page number (1-based). 1
itemsPerPage integer Optional Maximum number of items to return, up to a maximum of 100. 100
includeCount boolean Optional Specifies whether the response returns the totalCount field. true
pretty boolean Optional Indicates whether the response body should be in a prettyprint format. false
envelope boolean Optional

Flag indicating if Atlas should wrap the response in a JSON envelope.

Some API clients cannot access the HTTP response headers or status code. To remediate this, set envelope=true in the query.

For endpoints that return one result, the response body includes:

status HTTP response code
envelope Expected response body

For endpoints that return a list of results, the results object is an envelope. Atlas adds the status field to the response body.

false

Request Body Parameters

Body Parameter Type Necessity Description
databaseName string Required

Database against which Atlas authenticates the user. A user must provide both a username and authentication database to log into MongoDB.

Accepted values include:

  • admin if ldapAuthType, x509Type and awsIAMType are omitted or NONE.
  • $external if:
    • ldapAuthType is USER or GROUP, or
    • x509Type is MANAGED or CUSTOMER.
    • awsIAMType is USER or ROLE.
deleteAfterDate string Optional

Timestamp in ISO 8601 date and time format in UTC after which Atlas deletes the user. The specified date must be in the future and within one week of the time you make the API request.

Note

You may include an ISO 8601 time zone designator to ensure that the expiration date occurs with respect to the local time in the specified time zone.

labels array of documents Optional

Array containing key-value pairs that tag and categorize the database user.

Each key and value has a maximum length of 255 characters.

"labels": [
   {
     "key": "example key",
     "value": "example value"
   }
 ]

Note

The labels you define are not visible in the Atlas UI. They are returned in the response body when you use the Atlas API to get one, get all, or update a database user.

ldapAuthType string Optional

Method by which the provided username is authenticated. If no value is given, Atlas uses the default value of NONE.

Accepted values include:

NONE Atlas authenticates this user through SCRAM-SHA, not LDAP.
USER

LDAP server authenticates this user through the user’s LDAP user.

username must also be a fully qualified distinguished name, as defined in RFC 2253.

GROUP

LDAP server authenticates this user using their LDAP user and authorizes this user using their LDAP group. To learn more about LDAP security, see Set up User Authentication and Authorization with LDAP.

username must also be a fully qualified distinguished name, as defined in RFC 2253.

x509Type string Optional

X.509 method by which the provided username is authenticated. If no value is given, Atlas uses the default value of NONE.

The accepted types are:

NONE User doesn’t use X.509 authentication.
MANAGED

User to be used with Atlas-managed X.509.

Externally authenticated users must be created on the $external database.

CUSTOMER

User is being created for use with Self-Managed X.509. Users created with this x509Type require a Common Name (CN) in the username field. For more information, see RFC 2253.

Externally authenticated users must be created on the $external database.

awsIAMType string Optional

If this value is set, the new database user authenticates with AWS IAM credentials. If no value is given, Atlas uses the default value of NONE.

The accepted types are:

NONE The user does not use AWS IAM credentials.
USER New database user has AWS IAM user credentials.
ROLE New database user has credentials associated with an AWS IAM role.
groupId string Required Unique identifier of the Atlas project to which the user belongs.
roles array of documents Required

Array of this user’s roles and the databases / collections on which the roles apply. A role allows the user to perform particular actions on the specified database. A role on the admin database can include privileges that apply to the other databases as well.

Note

The available privilege actions for custom roles support a subset of MongoDB commands. See Unsupported Commands in M10+ Clusters for more information.

Important

If a user is assigned a custom role, they cannot be assigned any other roles.

roles
.collectionName
string Required

Collection for which the role applies.

You can specify a collection for the read and readWrite roles. If you do not specify a collection for read and readWrite, the role applies to all collections in the database (excluding some collections in the system. database).

Note

When applied to a collection, the read and readWrite roles in Atlas differ slightly from the built-in MongoDB read and readWrite roles.

In Atlas, read provides the following collection-level actions:

In Atlas, readWrite provides the same actions as read, as well as the following additional collection-level actions:

roles
.databaseName
string Required Database on which the user has the specified role. A role on the admin database can include privileges that apply to the other databases.
roles
.roleName
string Required

Name of the role. This value can either be a built-in role or a custom role.

The following accepted values of role are restricted to the admin database:

The following accepted values of role can be applied to a specific database:

The following accepted values of role can be applied to a specific collection:

  • read
  • readWrite

If you do not specify a collection for the read and readWrite roles, the roles apply to all collections (excluding some system. collections) in the database.

Note

If you specify a custom role name in this field, the roles.databaseName field must be admin.

scopes array of documents Optional

Array of clusters and Atlas Data Lakes that this user has access to. If omitted, Atlas grants the user access to all the clusters and Atlas Data Lakes in the project by default.

"scopes": [
  {
    "name": <resource-name>,
    "type": "CLUSTER"|"DATA_LAKE"
  }
]
scopes.name string Required Name of the cluster or Atlas Data Lake that the user has access to.
scopes.type string Required

Type of resource that the user has access to. Valid values are:

  • CLUSTER
  • DATA_LAKE
password string Conditional

User’s password. Required if:

  • x509Type, ldapAuthType, and awsIAMType are NONE, meaning that the database user authenticates with SCRAM-SHA.

This field is not included in the server response.

username string Required

Username for authenticating to MongoDB.

Must be a fully qualified distinguished name, as defined in RFC 2253, if:

  • ldapAuthType is USER or GROUP, or
  • x509Type is CUSTOMER.

Must be an Amazon Resource Name (ARN) if:

  • awsIAMType is USER or ROLE.

Response Elements

If you set the query element envelope to true, the response is wrapped by the content object.

Response Element Type Description
databaseName string

The user’s authentication database. A user must provide both a username and authentication database to log into MongoDB.

Returned values include:

  • $external if the user is authenticated using X.509 certificates, LDAP, or AWS IAM.

  • admin users authenticated using SCRAM-SHA.

    This is the default authentication scheme in Atlas.

deleteAfterDate string Timestamp in ISO 8601 date and time format in UTC after which Atlas deletes the user. This field is only present if an expiration date was specified when creating the entry.
groupId string Unique identifier of the Atlas project to which the user belongs.
labels array of documents Array containing key-value pairs that tag and categorize the database user.
ldapAuthType string

Method by which the specified username is authenticated. If no value is given, Atlas uses the default value of NONE.

Returned values include:

NONE Atlas authenticates this user through SCRAM-SHA, not LDAP.
USER LDAP server authenticates this user through the user’s LDAP user.
GROUP LDAP server authenticates this user using their LDAP user and authorizes this user using their LDAP group. To learn more about LDAP security, see Set up User Authentication and Authorization with LDAP.
x509Type string

X.509 method by which the provided username is authenticated. If no value is given, Atlas uses the default value of NONE.

The possible types are:

NONE The user does not use X.509 authentication.
MANAGED

The user is being created for use with Atlas-managed X.509.

Externally authenticated users can only be created on the $external database.

CUSTOMER

The user is being created for use with Self-Managed X.509. Users created with this x509Type require a Common Name (CN) in the username field. To learn more, see RFC 2253.

Externally authenticated users can only be created on the $external database.

awsIAMType string

If this value is set, the new database user authenticates with AWS IAM credentials.

Possible response values are:

NONE The user does not use AWS IAM credentials.
USER New database user has AWS IAM user credentials.
ROLE New database user has credentials associated with an AWS IAM role.
links document array One or more links to sub-resources and/or related resources.
roles string array Array of this user’s roles and the databases / collections on which the roles apply. A role allows the user to perform particular actions on the specified database. A role on the admin database can include privileges that apply to the other databases as well.
roles
.collectionName
string Collection on which the user has the specified role.
roles
.databaseName
string Database on which the user has the specified role. A role on the admin database can include privileges that apply to the other databases.
roles
.roleName
string

Name of the role. The accepted values are:

scopes array of documents Array of clusters and Atlas Data Lakes that this user has access to. Returns an empty array if the user has access to all the clusters and Atlas Data Lakes in the project. Database users are granted access to all resources by default.
scopes.name string Name of the cluster or Atlas Data Lake that this user has access to.
scopes.type string

Type of resource that this user has access to. Valid values are:

  • CLUSTER
  • DATA_LAKE
username string

Username for authenticating to MongoDB.

A fully qualified distinguished name, as defined in RFC 2253, is returned if:

  • ldapAuthType is USER or GROUP, or
  • x509Type is CUSTOMER.

An ARN is returned if:

  • awsIAMType is USER or ROLE.

Examples

Request

Create a database user that Atlas authenticates using SCRAM-SHA and the admin database.

Important

You must modify the following code block with the appropriate credentials and project ID.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \
     --header "Accept: application/json" \
     --header "Content-Type: application/json" \
     --include \
     --request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers" \
     --data '
       {
         "databaseName": "admin",
         "password": "changeme123",
         "roles": [{
           "databaseName": "sales",
           "roleName": "readWrite"
         }, {
           "databaseName": "marketing",
           "roleName": "read"
         }],
         "scopes": [{
           "name": "myCluster", 
           "type": "CLUSTER"
         }],
         "username": "david"
       }'

Create a database user that Atlas authenticates using X.509 certificates and the $external database.

Important

You must modify the following code block with the appropriate credentials and project ID.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \
     --header "Accept: application/json" \
     --header "Content-Type: application/json" \
     --include \
     --request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers" \
     --data '
       {
         "databaseName": "$external",
         "x509Type": "CUSTOMER",
         "roles": [{
           "databaseName": "sales",
           "roleName": "readWrite"
         }, {
           "databaseName": "marketing",
           "roleName": "read"
         }],
         "scopes": [{
           "name": "myCluster", 
           "type": "CLUSTER"
         }],
         "username": "CN=david@example.com,OU=users,DC=example,DC=com"
       }'

Create a database user that Atlas authenticates using LDAP and the $external database.

Important

You must modify the following code block with the appropriate credentials and project ID.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \
     --header "Accept: application/json" \
     --header "Content-Type: application/json" \
     --include \
     --request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers" \
     --data '
       {
         "databaseName": "$external",
         "ldapAuthType": "USER",
         "roles": [{
           "databaseName": "sales",
           "roleName": "readWrite"
         }, {
           "databaseName": "marketing",
           "roleName": "read"
         }],
         "scopes": [{
           "name": "myCluster", 
           "type": "CLUSTER"
         }],
         "username": "CN=david@example.com,OU=users,DC=example,DC=com"
       }'

Create a database user that Atlas authenticates using AWS IAM credentials and the $external database.

Important

You must modify the following code block with the appropriate credentials and project ID.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \
     --header "Accept: application/json" \
     --header "Content-Type: application/json" \
     --include \
     --request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers" \
     --data '
       {
         "databaseName": "$external",
         "awsIAMType": "USER",
         "roles": [{
           "databaseName": "sales",
           "roleName": "readWrite"
         }, {
           "databaseName": "marketing",
           "roleName": "read"
         }],
         "scopes": [{
           "name": "myCluster", 
           "type": "CLUSTER"
         }],
         "username": "arn:aws:iam::358363220050:user/mongodb-aws-iam-auth-test-user"
       }'

Response Header

HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=ISO-8859-1
Date: {dateInUnixFormat}
WWW-Authenticate: Digest realm="MMS Public API", domain="", nonce="{nonce}", algorithm=MD5, op="auth", stale=false
Content-Length: {requestLengthInBytes}
Connection: keep-alive
HTTP/1.1 201 Created
Vary: Accept-Encoding
Content-Type: application/json
Strict-Transport-Security: max-age=300
Date: {dateInUnixFormat}
Connection: keep-alive
Content-Length: {requestLengthInBytes}

Response Body

{
  "databaseName": "admin",
  "groupId": "{PROJECT-ID}",
  "labels": [],
  "ldapAuthType": "NONE",
  "links": [
    {
      "href": "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers/admin/david",
      "rel": "self"
    }
  ],
  "roles": [
    {
      "databaseName": "sales",
      "roleName": "readWrite"
    },
    {
      "databaseName": "marketing",
      "roleName": "read"
    }
  ],
  "scopes": [
    {
      "name": "myCluster",
      "type": "CLUSTER"
    }
  ],
  "username": "david",
  "x509Type": "NONE"
}
{
  "databaseName": "$external",
  "groupId": "{PROJECT-ID}",
  "labels": [],
  "ldapAuthType": "NONE",
  "awsIAMType" : "NONE",
  "links": [
    {
      "href": "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers/$external/CN=david@example.com,OU=users,DC=example,DC=com",
      "rel": "self"
    }
  ],
  "roles": [
    {
      "databaseName": "sales",
      "roleName": "readWrite"
    },
    {
      "databaseName": "marketing",
      "roleName": "read"
    }
  ],
  "scopes": [
    {
      "name": "myCluster",
      "type": "CLUSTER"
    }
  ],
  "username": "CN=david@example.com,OU=users,DC=example,DC=com",
  "x509Type": "CUSTOMER"
}
{
  "databaseName": "$external",
  "groupId": "{PROJECT-ID}",
  "labels": [],
  "ldapAuthType": "USER",
  "links": [
    {
      "href": "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers/$external/CN=david@example.com,OU=users,DC=example,DC=com",
      "rel": "self"
    }
  ],
  "roles": [
    {
      "databaseName": "sales",
      "roleName": "readWrite"
    },
    {
      "databaseName": "marketing",
      "roleName": "read"
    }
  ],
  "scopes": [
    {
      "name": "myCluster",
      "type": "CLUSTER"
    }
  ],
  "username": "CN=david@example.com,OU=users,DC=example,DC=com",
  "x509Type": "NONE",
  "awsIAMType" : "NONE"
}
{
  "databaseName": "$external",
  "groupId": "{PROJECT-ID}",
  "labels": [],
  "awsIAMType": "USER",
  "links": [
    {
      "href": "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers/$external/arn:aws:iam::358363220050:user%2Fmongodb-aws-iam-auth-test-user",
      "rel": "self"
    }
  ],
  "roles": [
    {
      "databaseName": "sales",
      "roleName": "readWrite"
    },
    {
      "databaseName": "marketing",
      "roleName": "read"
    }
  ],
  "scopes": [
    {
      "name": "myCluster",
      "type": "CLUSTER"
    }
  ],
  "username": "arn:aws:iam::358363220050:user/mongodb-aws-iam-auth-test-user",
  "x509Type": "NONE",
  "ldapAuthType" : "NONE"
}