Atlas
MongoDB Developer Center
chevron-right
Developer Topics
chevron-right
Products
chevron-right
Atlas
chevron-right

Create a REST API with Cloudflare Workers, MongoDB Atlas, and Realm

Luke Edwards, Maxime BeugnetPublished Nov 15, 2021 • Updated May 09, 2022
ServerlessCloudflareAtlasTypeScript
facebook icontwitter iconlinkedin icon
random alt
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

Cloudflare Workers
provides a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.
MongoDB Atlas
allows you to create, manage, and monitor MongoDB clusters in the cloud provider of your choice (AWS, GCP, or Azure) while
MongoDB Realm
Application can provide a layer of authentication and define access rules to the collections.
In this blog post, we will combine all these technologies together and create a REST API with a Cloudflare worker using the
MongoDB Realm Web SDK
and a MongoDB Atlas cluster to store the data.

TL;DR!

The worker is in this
GitHub repository
. The
README
will get you up and running in no time, if you know what you are doing. Otherwise, I suggest you follow this step-by-step blog post. ;-)

Prerequisistes

  • NO credit card! You can run this entire tutorial for free!
  • Git
    and
    cURL
    .
  • Cloudflare
    account (free plan is fine) with a *.workers.dev subdomain for the workers. Follow
    steps 1 to 3 from this documentation
    to get everything you need.
We will create the Realm application together in the next section. This will provide you the Realm AppID and API key that we need.
To deploy our Cloudflare worker, we will need:
  • The
    Realm application ID
    (top left corner in your Realm app—see next section).
  • The Cloudflare account login/password.
  • The Cloudflare account ID (in Workers tab > Overview).
To test (or interact with) the REST API, we need:
  • The Realm authentication API key (more about that below, but it's in Authentication tab > API Keys).
  • The Cloudflare *.workers.dev subdomain (in Workers tab > Overview).
It was created during this step of your set-up:

Create and Configure the Realm Application

To begin with, head to your MongoDB Atlas main page where you can see your cluster and access the Realm tab at the top.
Create an empty Realm application
(no template) as close as possible to your MongoDB Atlas cluster to avoid latency between your cluster and Realm app. My Realm app is "local" in Ireland (eu-west-1) in my case.
Now that our Realm app is created, we need to set up two things: authentication via API keys and collection rules. Before that, note that you can retrieve your Realm app ID in the top left corner of your new application.
Authentication Via API Keys
Head to Authentication > API Keys.
Activate the provider and save the draft.
We need to create an API key, but we can only do so if the provider is already deployed. Click on review and deploy.
Now you can create an API key and save it somewhere! It will only be displayed once. If you lose it, discard this one and create a new one.
We only have a single user in our application as we only created a single API key. Note that this tutorial would work with any other authentication method if you update the authentication code accordingly in the worker.
Collection Rules
By default, your Realm application cannot access any collection from your MongoDB Cluster. To define how users can interact with the data, you must
define roles and permissions
.
In our case, we want to create a basic REST API where each user can read and write their own data in a single collection todos in the cloudflare database.
Head to the Rules tab and let's create this new cloudflare.todos collection.
Each document will belong to a unique user defined by the owner field. This field will contain the user ID that you can see in the App Users tab.
You can now click one more time on Review Draft and Deploy. Our Realm application is now ready to use.

Set Up and Deploy the Cloudflare Worker

The Cloudflare worker is available in
GitHub repository
. Let's clone the repository.
Now that we have the worker template, we just need to change the configuration to deploy it on your Cloudflare account.
Edit the file wrangler.toml:
  • Replace CLOUDFLARE_ACCOUNT_ID with your real Cloudflare account ID.
  • Replace MONGODB_REALM_APPID with your real MongoDB Realm app ID.
You can now deploy your worker to your Cloudflare account using
Wrangler
:
Head to your Cloudflare account. You should now see your new worker in the Workers tab > Overview.

Check Out the REST API Code

Before we test the API, please take a moment to read the
code of the REST API
we just deployed, which is in the src/index.ts file:

Test the REST API

Now that you are a bit more familiar with this REST API, let's test it!
Note that we decided to pass the values as parameters and the authorization API key as a header like this:
You can use
Postman
or anything you want to test your REST API, but to make it easy, I made some bash script in the api_tests folder.
In order to make them work, we need to edit the file api_tests/variables.sh and provide them with:
  • The Cloudflare worker URL: Replace YOUR_SUBDOMAIN, so the final worker URL matches yours.
  • The MongoDB Realm app API key: Replace YOUR_REALM_AUTH_API_KEY with your Realm auth API key.
Finally, we can execute all the scripts like this, for example:
As you can see, the REST API works like a charm!

Wrap Up

Cloudflare offers a Workers
KV
product that can make for a quick combination with Workers, but it's still a simple key-value datastore and most applications will outgrow it. By contrast, MongoDB is a powerful, full-featured database that unlocks the ability to store, query, and index your data without compromising the security or scalability of your application.
As demonstrated in this blog post, it is possible to take full advantage of both technologies. As a result, we built a powerful and secure serverless REST API that will scale very well.
If you have questions, please head to our
developer community website
where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB. If your question is related to Cloudflare, I encourage you to join their
active Discord community
.

Copy Link
facebook icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article
Automate the Automation on MongoDB Atlas

May 13, 2022
Tutorial
Calling the MongoDB Atlas Administration API: How to Do it from Node, Python, and Ruby

May 27, 2022
Tutorial
Static Website Deployments to MongoDB with Hugo, Git, and Travis CI

May 16, 2022
Tutorial
Visually Showing Atlas Search Highlights with JavaScript and HTML

May 16, 2022
Table of Contents