Using realm-cli to setup an https endpoint

I’m using v. 2.6.2 of the mongodb-realm-cli library and trying to programmatically setup a basic HTTPS endpoint, following the documentation in https://www.mongodb.com/docs/atlas/app-services/data-api/custom-endpoints/.

I’ve got a new function, which is successfully created when running the push command, but the https_endpoints->config.json configuration file has no effect to the infrastructure and no errors are reported. How can I create the https endpoint for the above function then? Setting it up manually via the UI does work, but nothing happens when trying the same via the realm-cli…

For reference my config.json file is similar to the following:

[
  {
    "route": "/testingonly5",
    "http_method": "GET",
    "function_name": "testingonly",
    "validation_method": "NO_VALIDATION",
    "respond_result": true,
    "fetch_custom_user_data": false,
    "create_user_on_auth": false,
    "disabled": false,
    "return_type": "JSON"
  }
]

Any suggestions please?

Hi @George_Ivanov - Welcome to the community!

I’ve got a new function, which is successfully created when running the push command, but the https_endpoints->config.json configuration file has no effect to the infrastructure and no errors are reported.

Setting it up manually via the UI does work, but nothing happens when trying the same via the realm-cli…

Just to clarify - It sounds like the push itself is successful but you are not seeing the changes to the HTTPS Endpoint configuration after the push. In addition to this, I presume the function itself testingonly is already created and you’re only trying to create the HTTPS endpoint to associate it with this function. Please correct me if I am incorrect in either of my assumptions.

Regarding the push itself, are you seeing something similar to the below output?

? Please confirm the changes shown above Yes
Creating draft
Pushing changes
Deploying draft
Deployment complete
Successfully pushed app up: appname123-0-abcde

Regards,
Jason

1 Like

Hi @Jason_Tran,

Yes, the output from the push is:

executing: npx realm-cli login --api-key ****** --private-api-key ****** -y
Successfully logged in
executing: npx realm-cli push --remote platform-dev-**** --local build/mongodb-realm-app -y 
Determining changes
Creating draft
Pushing changes
Deploying draft
Deployment complete
Successfully pushed app up: platform-dev-***
deployed platform-dev-*******

The function is created:


But no https endpoint is added

Thanks for providing the output George.

Due to the -y option used, I believe the proposed changes won’t be logged to the output in the cli. Would you be able to try create another HTTPS_endpoint again and provide the output without the -y option? Additionally, could you try creating the HTTPS_endpoint directly from the application folder (or even a test application folder) without the use of --remote or --local for troubleshooting purposes? This will help determine what the issue may be when creating the custom endpoint.

An example of this below from my test environment where a HTTPS_Endpoint is created (original config.json only containing []):

../realm-cli push
Determining changes
The following reflects the proposed changes to your Realm app
--- http_endpoints/config.json
+++ http_endpoints/config.json
@@ -1,2 +1,14 @@
-[]
+[
+    {
+        "route": "/customroute",
+        "http_method": "POST",
+        "function_name": "helloworld",
+        "validation_method": "NO_VALIDATION",
+        "respond_result": true,
+        "fetch_custom_user_data": false,
+        "create_user_on_auth": false,
+        "disabled": false,
+        "return_type": "JSON"
+    }
+]


? Please confirm the changes shown above Yes
Creating draft
Pushing changes
Deploying draft
Deployment complete
Successfully pushed app up: application-1-redacted

Also, regarding the following:

--local build/mongodb-realm-app

Can you provide the following output from the above directory?:

  1. ls -l
  2. ls -l https_endpoints
  3. cat https_endpoints/config.json

Please redact any personal or sensitive information before posting here.

Regards,
Jason

Still no luck, only the function is detected when applying, while the https_endpoints are completely ignored:

georivan@pcpcpc mongodb-realm-app % realm-cli push --remote data-platform-dev-********
Determining changes
The following reflects the proposed changes to your Realm app
--- functions/testingonly/config.json
+++ functions/testingonly/config.json
@@ -1 +1,6 @@
+{
+    "can_evaluate": {},
+    "name": "testingonly",
+    "private": false
+}
 

--- functions/testingonly/source.js
+++ functions/testingonly/source.js
@@ -1 +1,7 @@
+exports = () => {
+    const commitId = context.values.get("gitCommitId");
+    const commitMessage = context.values.get("gitCommitMessage");
+    const commitTime = context.values.get("gitCommitTime");
+    return { commitId, commitMessage, commitTime };
+};
 

? Please confirm the changes shown above (y/N) 

The folder structure is:

georivan@pcpcpc mongodb-realm-app % ls -l
total 8
drwxr-xr-x  2 georivan  staff   64 Nov 29 11:27 auth
drwxr-xr-x  3 georivan  staff   96 Nov 29 11:30 auth_providers
-rw-r--r--  1 georivan  staff  326 Nov 29 11:27 config.json
drwxr-xr-x  3 georivan  staff   96 Nov 29 11:27 data_sources
drwxr-xr-x  7 georivan  staff  224 Nov 29 11:27 environments
drwxr-xr-x  3 georivan  staff   96 Nov 29 11:29 functions
drwxr-xr-x  3 georivan  staff   96 Nov 29 11:29 graphql
drwxr-xr-x  3 georivan  staff   96 Nov 29 11:51 https_endpoints
drwxr-xr-x  2 georivan  staff   64 Nov 29 11:27 log_forwarders
drwxr-xr-x  3 georivan  staff   96 Nov 30 17:38 services
drwxr-xr-x  2 georivan  staff   64 Nov 29 11:27 sync
drwxr-xr-x  2 georivan  staff   64 Nov 29 11:30 triggers
drwxr-xr-x  7 georivan  staff  224 Nov 29 11:32 values

Contents of https_endpoints folder:

georivan@pcpcpc mongodb-realm-app % ls -l https_endpoints
total 8
-rw-r--r--  1 georivan  staff  293 Nov 29 11:51 config.json

Finally, the config file itself:

georivan@pcpcpc https_endpoints % cat config.json
[
  {
    "route": "/testingonly5",
    "http_method": "GET",
    "function_name": "testingonly",
    "validation_method": "NO_VALIDATION",
    "respond_result": true,
    "fetch_custom_user_data": false,
    "create_user_on_auth": false,
    "disabled": false,
    "return_type": "JSON"
  }
]

Can you try rename the directory to "http_endpoints" and try the push again?

Regards,
Jason

I’ve tried both http_endpoints and https_endpoints as the directory name, but again makes no difference :frowning:

If possible, would you be able to pull the Application down again? From my testing, the old directory / folder was somehow being tracked still:

$ ls -la
total 8
drwxr-xr-x  12   staff  384  1 Dec 20:14 .
drwxr-xr-x  21   staff  672  1 Dec 10:59 ..
drwxr-xr-x   4   staff  128  1 Dec 10:59 auth
drwxr-xr-x   3   staff   96  1 Dec 10:59 data_sources
drwxr-xr-x   7   staff  224  1 Dec 10:59 environments
drwxr-xr-x   4   staff  128  1 Dec 10:59 functions
drwxr-xr-x   4   staff  128  1 Dec 10:59 graphql
drwxr-xr-x   3   staff   96  1 Dec 10:59 https_endpoints /// <--- Changed directory to https_endpoints manually
drwxr-xr-x   2   staff   64  1 Dec 10:59 log_forwarders
-rw-r--r--   1   staff  201  1 Dec 10:59 realm_config.json
drwxr-xr-x   3   staff   96  1 Dec 10:59 sync
drwxr-xr-x   2   staff   64  1 Dec 10:59 values

After running a push, the previous "http_endpoints" directory is still being tracked (will need to see why this is):

$ ../realm-cli push
Determining changes
The following reflects the proposed changes to your Realm app
--- http_endpoints/config.json
+++ http_endpoints/config.json
@@ -1,14 +1,2 @@
-[
-    {
-        "route": "/testingonly",
-        "http_method": "GET",
-        "function_name": "helloworld",
-        "validation_method": "NO_VALIDATION",
-        "respond_result": true,
-        "fetch_custom_user_data": false,
-        "create_user_on_auth": false,
-        "disabled": false,
-        "return_type": "EJSON"
-    }
-]
+[]

What I tried next:

  1. realm-cli pull which created the "http_endpoints" folder:
$ ../realm-cli pull
? Directory '/Users/<REDACTED>/mongodb-realm-cli/node_modules/mongodb-realm-cli/Application-1' already exists, do you still wish to proceed? Yes
Saved app to disk
Successfully pulled app down: .
❯ ls
total 8
drwxr-xr-x  4   staff  128  1 Dec 10:59 auth
drwxr-xr-x  3   staff   96  1 Dec 10:59 data_sources
drwxr-xr-x  7   staff  224  1 Dec 10:59 environments
drwxr-xr-x  4   staff  128  1 Dec 10:59 functions
drwxr-xr-x  4   staff  128  1 Dec 10:59 graphql
drwxr-xr-x  3   staff   96  1 Dec 20:20 http_endpoints /// <--- Created from the pull
drwxr-xr-x  3   staff   96  1 Dec 10:59 https_endpoints /// <--- Manually changed prior to pull
drwxr-xr-x  2   staff   64  1 Dec 10:59 log_forwarders
-rw-r--r--  1   staff  201  1 Dec 20:20 realm_config.json
drwxr-xr-x  3   staff   96  1 Dec 10:59 sync
drwxr-xr-x  2   staff   64  1 Dec 10:59 values
  1. realm-cli push and the changes were back to normal

It may be possibly easier to just remove the directory and do a fresh pull of the application, make the changes from the "http_endpoints" directory that is created, and perform a push again.

Let me know if both a fresh pull / push doesn’t work either.

Regards,
Jason

Downloading the structure using a pull and then adding the http_endpoints (https_endpoints is not tracked/does not work as you’ve suggested) has done the job!

I’ve noticed that the project structure is a bit different now compared to what it was like with v.1 of the realm-cli. However it’s easy enough to move the necessary files around and update some of the config files to match the new style!

Thanks!

1 Like