Create a REST API with Cloudflare Workers, MongoDB Atlas, and Realm
Rate this tutorial
- NO credit card! You can run this entire tutorial for free!
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 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.devsubdomain (in Workers tab > Overview).
It was created during this step of your set-up:
To begin with, head to your MongoDB Atlas main page where you can see your cluster and access the Realm tab at the top.
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.
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.
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
Head to the Rules tab and let's create this new
Each document will belong to a unique user defined by the
ownerfield. This field will contain the user ID that you can see in the
You can now click one more time on
Review Draft and Deploy. Our Realm application is now ready to use.
Now that we have the worker template, we just need to change the configuration to deploy it on your Cloudflare account.
Edit the file
CLOUDFLARE_ACCOUNT_IDwith your real Cloudflare account ID.
MONGODB_REALM_APPIDwith your real MongoDB Realm app ID.
Head to your Cloudflare account. You should now see your new worker in the Workers tab > Overview.
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:
In order to make them work, we need to edit the file
api_tests/variables.shand 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_KEYwith 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!
Cloudflare offers a Workers
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.
Calling the MongoDB Atlas Administration API: How to Do it from Node, Python, and Ruby
May 27, 2022