Call MongoDb Realm Function from external service using HTTP Basic Authentication

Hi! One of the services I am using for email marketing (Vero) allows you to pull in data via an API. What I want to be able to do is to use a MongoDbRealm function build that api allowing Atlas data to be included in my customers emails.

Unforunately the service only supports HTTP Basic Authentication (http://username:password@yoururl.com/data-feed.json). From what I am not sure this is supported by MongoDbRealm. Ideally I would use API keys but it doesn’t seem to be supported. Can custom function authentication be used for this? If so how?

Hi @Simon_Persson,

I would like to help but I don’t think I understand the use case…

You need a function to execute a simple auth http request to your email service?

The context.http should be able to do it easily:

If you need further guidance describe your requirements and link to your realm application.

Best regards,
Pavel

Let me see if I can explain it better.

I basically want to do the opposite. I want the external service to be able to pull data from MongoDb via a MongoDb Realm function. And the external service only supports executing API calls using Http Basic Auth.

Some additional details on the use case:
Vero is email automation service. Based on user actions it can trigger sending emails. To personalize these emails it can execute an api call just prior to sending the message and populate the email with data. So I basically want Vero to be able to fetch data from MongoDb Realm before sending the email.

Did that make the use case more clear? Is it possible to do this?

Hi @Simon_Persson,

Well it is possible if you expose the data via a webhook function.

You can use a secret or use application authentication (which is similar to basic auth with a predefined username and password)

Now in the webhook function you can query your Atlas service and use response.setBody(....) to return a JSON you need.

Specify the webhook url with creds in your email service.

Let me know if that helps.

Kind regards
Pavel

I have something similar. Unfortunately the documentation is not very detailed and Realm still appears kind of Beta-ish to me. I am not sure if my solution is something Mongo advises. They seem bent on pushing GraphQL, but I want a “RESTful” API.

Instead of a Real, “Function”, here is what I have been doing
Select “3rd Partly Services”
Click the “Add Service” button.
Select HTTP and give it a name (RESTService)
Click the “Add incoming webHook” button
Chose an HTTP Method you wish to expose (i.e. GET)
Click the Function Editor Tab
Write you function to extract the data you need

To execute this webHook, copy webhookUrl and add your parms
Use the browser, Postman, Fiddler or whatever gives you the most pleasure :slight_smile:

Note: You can add authentication, but that’s a whole different topic

Here is an example function I wrote to extract data:

exports = async function getStudents(payload, response) {
const {accountId } = payload.query;
const db = context.services.get(“mongodb-atlas”).db(“studentsDB”);
const students = db.collection(“students”);
console.log(getStudents accountId:${accountId});

const query = { accountId:accountId };
const projection = { "_id": 1, "studentId":"$_id", "firstName":"$firstName","lastName":"$lastName",  
 "accountId":"$accountId"};
response.setHeader("Content-Type","application/json");

await students.find(query, projection).toArray()
.then(result => {
    if(result) {
      response.setStatusCode(200);
      //response.setBody(`{"students":${result}`);
      response.setBody(`{"students":${JSON.stringify(result)}}`);
    }
    else {
      console.log("students not found:",JSON.stringify(result));
      response.setStatusCode(404);
      response.setBody(`{message:"No students not found for given criteria"}`);
    }
}).catch(err => {
  console.log("error getting students:",err);
  response.setStatusCode(500);
  response.setBody(`{error:${err}}`);
})

}

“3rd Party Service” is a misnomer in my suggestion because your Mongo DB instance is the 3rd Party.

Hope this helps

You are not actually calling a 3rd Party Service, but instead

2 Likes

Hi @Herb_Ramos,

Thanks for the example. Realm Webhooks were formally known as Stitch webhooks and are GA for over 2 years now.

Since the engine is versatile its hard to document all use case types. We are constantly working on improving and adding templates and snippets.

@Simon_Persson if you have issues porting this example for your use let us know.

Kind regards
Pavel

1 Like

Thank you! I’ll definitely give the webhooks a try and report back :wink: I have been on Realm for years, but I am new to Atlas… Excited about what the cloud functions and webhooks bring to the table :slight_smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.