2 / 4
Nov 2024

Hey, I’m trying to create a database user in MongoDB Atlas using the Administrative API, but I keep encountering a 401 Unauthorized error.

Here’s the command I’m using:

curl --request POST “https://cloud.mongodb.com/api/atlas/v1.0/groups//databaseUsers”
–user “:”
–header “Content-Type: application/json”
–data ‘{
“databaseName”: “$external”,
“groupId”: “”,
“roles”: [
{
“databaseName”: “test”,
“roleName”: “readWrite”
}
],
“username”: “CN=test_user”,
“x509Type”: “CUSTOMER”
}’

The API keys I’m using have Project Owner access, I get the following

error:{ "error" : 401, "reason" : "Unauthorized", "detail" : "You are not authorized for this resource." }

Hi Chris, Thanks I was able to get this sorted by running the below command.

curl --user "<private>:<public>" --digest \ --header "Content-Type: application/json" \ --include \ --request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/<group-id>/databaseUsers" \ --data '{ "databaseName": "$external", "roles": [ { "databaseName": "test", "roleName": "readWrite" } ], "username": "CN=test_user", "x509Type": "CUSTOMER" }'

Also, is there a mechanism to specify the collection name as well? I couldn’t find it here in this document.
https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Database-Users/operation/updateDatabaseUser