Interested in speaking at MongoDB World 2022? Click here to become a speaker.
HomeLearnHow-toCalling the MongoDB Atlas Admin API - How to do it from Node, Python, and Ruby

Calling the MongoDB Atlas Admin API - How to do it from Node, Python, and Ruby

Updated: Nov 30, 2021 |

Published: Nov 30, 2021

  • Atlas
  • JavaScript
  • Python
  • ...

By DJ Walker

Rate this article

The real power of a cloud-hosted, fully managed service like MongoDB Atlas is that you can create whole new database deployment architectures automatically, using the services API. Getting to the MongoDB Atlas Admin API is relatively simple and, once unlocked, it opens up a massive opportunity to integrate and automate the management of database deployments from creation to deletion. The API itself is an extensive REST API, there's role-based access control and you can have user or app-specific credentials to access it.

There is one tiny thing that can trip people up though. The credentials have to be passed over using the digest authentication mechanism, not the more common basic authentication or using an issued token. Digest authentication, at its simplest, waits to get an HTTP 401 (not authorized) from the web endpoint. That response comes with data and the client then sends an encrypted form of the username and password as a digest and the server works with that.

And that's why we are here today, to show you how to do that with the least fuss in Python, Node, and Ruby. In each example, we'll try and access the base URL of the Atlas Admin API which returns a JSON document about the underlying applications name, build and other facts.


We start with the simplest and most self-contained example, Python.

1import os
2import requests
3from requests.auth import HTTPDigestAuth
4import pprint
6baseurl = ""
8response=requests.get(baseurl, auth=HTTPDigestAuth(
9 os.environ["ATLAS_USER"], os.environ["ATLAS_USER_KEY"]))

In the Python version, we lean on the requests library for most of the heavy lifting. As well as importing requests we also bring in HTTPDigestAuth from requests' auth module to handle digest authentication. The os import is just there so we can get the environment variables ATLAS_USER and ATLAS_USER_KEY as credentials and the pprint import is just to format our results.

The critical part is the addition of auth=HTTPDigestAuth(...) to the requests.get() call. This installs the code needed to respond to the server when it asks for the digest.

If we now run this program...

We have our API response.


For Node.js, it is a matter of finding a package which does HTTP fetching with digest authentication. There's a whole ecosystem of packages out there and one that we found that worked was digest-fetch which wraps the very popular node-fetch library with what it needs to do HTTP Digest authentication. After installing the required libraries

1npm install digest-fetch crypto-js node-fetch --save

We can now create our code:

1const DigestFetch = require("digest-fetch");
3const client = new DigestFetch(
6{ }
9const urlbase = "";
13.then(res => res.json())
14.then(json => console.log(JSON.stringify(json,null," ")));

Taking it from the top... We first require and import the digest-fetch package. It'll pull in the other needed packages. Then we create our DigestFetch client, giving it the user and key values from the environment and an empty set of options. After setting the API's URL base value we then processed to client.fetch() from it. We're using promises and so the next two thens see the data converted to JSON and then pretty-printed on the console. And we're ready to run:

On to our final language...


HTTParty is a widely used Gem which is used by the Ruby and Rails community to perform HTTP operations. It also, luckily, supports digest authentication so, first up, gem install httparty to get the party started. There are two ways to use HTTParty, one is creating an object which abstracts the calls away while the other is just directly calling methods on HTTParty itself. For brevity, we'll do the latter. Here's the code:

1require 'httparty'
2require 'json'
7result=HTTParty.get(baseurl, { :digest_auth => auth } )
9pp JSON.parse(result.body())

We require in the HTTParty and json gems first. We then create a dictionary with our username and key, mapped for HTTParty's authentication and set a variable to hold the base URL. We're ready to do our get now, and in the options - the second parameter of the get -we pass :digest_auth=>auth to switch on the digest support. We wrap up by JSON parsing the resulting body and pretty printing that. Put it all together and run it and we get:

#Next Stop - The API

With the authentication taken care of - just remember to be fastidious with your API key security and make sure you revoke unused keys - you can now move on to explore the API itself. Start in the documentation and see what you can automate today.

Rate this article
MongoDB logo
© 2021 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.