GIANT Stories at MongoDB

Creating a Data Enabled API in 10 Minutes with MongoDB Stitch

Creating an API that exposes data doesn’t have to be complicated. With MongoDB Stitch, you can create a data enabled endpoint in about 10 minutes or less.

At the heart of the entire process is MongoDB Stitch’s Services. There are several from which to choose and to create a data enabled endpoint, you’ll choose the HTTP Service with a Webhook.

Adding a Stitch Service

When you create an HTTP Service, you’re enabling access to this service from Stitch’s serverless functions in the form of an object called More on that later when we create a serverless function attached to this service.

Name and add the service and you’ll then get to create an “Incoming Webhook”. This is the process that will be contacted when your clients request data of your API.

Call the webhook whatever you like, and set the parameters as you see below:

We’ll create this API to respond with results to GET requests. Next up, you’ll get to create the logic in a function that will be executed whenever your API is contacted with a GET request.

Defining the Function

Before we modify this script to return data, let’s take a look at the Settings tab — this is where you’ll find the URL where your clients will reach your API.

That’s it — you’ve configured your API. It’s not going to do anything interesting. In fact, the default responds to requests with “Hello World”. Let’s add some data.

Assuming we have a database called mydatabase and a collection of contact data called mycollection, let’s write a function for our service:

Creating a function to return data from a collection in MongoDB Stitch

And here’s the source:

exports = function(payload) {
 const mongodb =“mongodb-atlas”);
 const mycollection = mongodb.db(“mydatabase”).collection(“mycollection”);
 return mycollection.find({}).toArray();

This exposes all documents in the database whenever a client calls the webhook URL associated with our HTTP Service. That’s it.

Let’s use Postman to show how this works. Grab your API Endpoint URL from the service settings screen. Mine is as follows — yours will differ.

Paste that into the GET URL field and hit Send, you should see something similar to the following:

Check out the GitHub Repository to review the code and try it yourself and review the screencast where I create a data enabled API in 10 Minutes with MongoDB Stitch.

Want to try this for yourself? Sign up for a free MongoDB Atlas account. Looking to leverage an API for integration with MongoDB? Read Andrew Morgan’s article on Building a REST API with MongoDB Stitch.

Additional Resources:

How To Pause and Resume Atlas Clusters

Last week we showed you how to list the resources associated with your MongoDB Atlas environment via a simple Python program. Let’s extend this program this week with a more useful feature, the ability to pause and resume clusters. We can use the Atlas Management API to do this via the “Pause Cluster” menu entry.

Pause a Cluster in MongoDB Atlas

However, when we pause a cluster the Atlas environment will restart the cluster after seven days. Also, both pausing and resuming require a login, navigation etc. Basically, it’s a drag to do this regularly. If you are running clusters for development they are rarely required late at night or at weekends.

It would be great to have a simple script to pause and resume these clusters using the project ID and cluster name. Then we could run this script in crontab or our own favorite scheduling program and pause and resume clusters on a defined schedule. We have rewritten the script to do exactly that.

The extended script allows you to both list resources and pause and/or resume clusters using their project ID and cluster name.

$ python -h
usage: [-h] [--username USERNAME] [--apikey APIKEY]
                           [--project_id PROJECT_ID] [--org_id ORG_ID]
                           [--pause PAUSE_CLUSTER_NAME]
                           [--resume RESUME_CLUSTER_NAME] [--list]

optional arguments:
  -h, --help            show this help message and exit
  --username USERNAME   MongoDB Atlas username
  --apikey APIKEY       MongoDB Atlas API key
  --project_id PROJECT_ID
                        specify project for cluster that is to be paused
  --org_id ORG_ID       specify an organisation to limit what is listed
                        pause named cluster in project specified by
                        resume named cluster in project specified by
  --list                List of the complete org hierarchy

To pause a cluster just run:

$ python --list --org_id XXXXXXXXXXXXXXXXXXXX175c
 1. Org  : 'Open Data at MongoDB',   id=XXXXXXXXXXXXXXXXXXXX175c
  1. Proj : 'JD Stitch Demos',        id=XXXXXXXXXXXXXXXXXXXXcb08
   1. cluster: 'stitch',                 id=XXXXXXXXXXXXXXXXXXXX5697 paused=True
  2. Proj : 'MUGAlyser',              id=XXXXXXXXXXXXXXXXXXXX9bab
   1. cluster: 'MUGAlyser',              id=XXXXXXXXXXXXXXXXXXXXbfba paused=False
  3. Proj : 'Open Data',              id=XXXXXXXXXXXXXXXXXXXX8010
   1. cluster: 'Utility',                id=XXXXXXXXXXXXXXXXXXXX1a03 paused=True
   2. cluster: 'MOT',                    id=XXXXXXXXXXXXXXXXXXXX94dd paused=False
   3. cluster: 'Foodapedia',             id=XXXXXXXXXXXXXXXXXXXX9fbf paused=False
   4. cluster: 'UKPropertyPrices',       id=XXXXXXXXXXXXXXXXXXXX7ac5 paused=False
   5. cluster: 'New-York-Taxi',          id=XXXXXXXXXXXXXXXXXXXXa18a paused=False
   6. cluster: 'demodata',               id=XXXXXXXXXXXXXXXXXXXX2cf8 paused=False
(We have hidden the real resource IDs behind X’s).

To get the project ID look for the id field for the Proj entry. To get the cluster name just look for the string in quotes after the cluster identifier. We have highlighted the project ID and the cluster name we are going to use.

Now to pause the cluster just run:

$ python --project_id XXXXXXXXXXXXXXXXXXXX9bab --pause MUGAlyser
Pausing cluster: 'MUGAlyser'

To resume a cluster just use the --resume argument instead of the --pause argument. Want to pause or resume more than one cluster in a single project? You can, just by adding multiple --pause or --resume arguments.

Now, you just need to add this script to your favourite scheduler. Note for this example I have already set the environment variables ATLAS_USERNAME and ATLAS_APIKEY so we don’t need to pass them in on the command line.

Now go save some money on your development clusters. Your boss will thank you!