Run Realm function from realm-cli

Is there a way to run Realm cloud function from within the realm-cli?

realm-cli is mostly meant for importing and exporting app configurations (basically, creating, updating, and deleting apps), and doesn’t have any means of running a Realm cloud function, unfortunately. I can think of a few different ways to implement command-line execution of Realm functions, though:

  1. Via a client SDK (you can create a CLI app using the Node.js or .NET SDKs if your workflow requires CLI).
  2. Connect your function to a webhook to run the function when you hit a certain endpoint of your Realm app.
  3. Run a function for testing via the Realm Admin API (note that this isn’t suitable for production use, but if you just want to execute a function via a command line interface on your own machine, this could work).

Can you call a Realm function from the Admin API even if it is in private mode?

I’m not sure what you mean by “private mode.” The Admin API always requires authentication, and you’ll have to write that logic yourself, so it’s probably the hardest of these three methods to implement. Personally I would recommend the webhook method with a small CLI utility that uses your favorite HTTP library.

Yes, I understand that, I mean functions have this configuration, for example:

{
    "name": "processCsv",
    "private": false
}

In my case, my functions are all private true so it wont be called from client-based code with the Realm SDK.

I’ve posted on StackOverflow, the idea of what I am trying to achieve:

Aha, now I see what you mean. Sorry for misunderstanding earlier.

For this use case, I would definitely recommend using the Admin API endpoint. I’m sure you can encode the authentication logic into your IDEA plugin. And since you’re trying to build something that lets users test functions, the Admin API endpoint is perfect (since it’s meant for testing functions).

The function privacy setting shouldn’t matter in this case. See the documentation on the function privacy setting – basically, setting a function to “private” stops users from calling your function directly using the Realm SDK. This is useful for situations where you want to write logic for some privileged functionality (like updating a user’s custom data, or administrating something) but you don’t want to expose that functionality to all users through a typical function call.

The privacy setting doesn’t impact running a function through the Admin API. The Admin API is an administrative feature by nature – you can use it to add, update, and remove logic, functions, rules, etc. in the backend. If you think about it, you could use the Admin API to set any function to “private: false” and then run it anyway… so it doesn’t even make sense for the privacy setting to apply there. So that should work well for your use case. Let me know if you hit any other snags! This sounds like a really cool project and is something I’ve actually wished for myself in the past when developing projects with Realm.

1 Like

I started working with the plugin, so far what I have figured out that is required by the API are:

username
apiKey
groupId
appId

All is good except for the groupId since there’s no group id that is part of the exported realm app, the appId can be found from the exported, the userame/apiKey can be configured on the Plugin setting form manually.

Where can the groupId be found? or can it be part of the exported application?


My plan for the plugin is you open an exported realm app using Webstorm or IntelliJ the the plugin will figure out the function automatically from the config.json files on the functions folder.

Creating functions also through context menu is planned. Right click then create function, configuring the input data, name, privacy, etc.

And depending which source is open a custom run terminal it can trigger the cloud function based on the parameters typed, similar to the console of Realm cloud.

If you open up the Realm UI in a browser, take a look at the URL. It should look something like this:

https://realm.mongodb.com/groups/<group id>/apps/<app id>/values

(I know, I know – it’s confusing that the App ID for the Admin API is not the same as the App ID for the client SDKs. Sorry about that).

You should be able to extract your group and app ids from there to use in the Admin API. I suppose your users might have to do that same process manually, but there also might be a way to get them programmatically if that sounds like a huge inconvenience.

I believe the group ID actually identifies your Atlas organization.

I see, okay I will try this.