Configure Service Rules

On this page


In order to call a service action, you must first define a service rule that enables and configures the capabilities of the action. You can define service rules from the Realm UI or by importing a service configuration directory that contains a rule configuration file. Select the tab below that corresponds to the method you want to use.



Create a New Rule

You can create and configure a service rule from its associated service page in the Realm UI.

To define a new service rule:

  1. Click Services in the left-hand navigation.
  2. Click on the service that you want to specify a rule for.
  3. Click on the Rules tab of the service.
  4. Click New Rule.
  5. Specify a name for the rule in the textbox that appears.
  6. Click Add Rule to confirm the creation of the new rule.

Configure the Rule

To configure the new service rule you just created:

  1. Click on the newly created rule in the rules list on the left-hand side of the page.

  2. In the list of Actions, select all of the actions that you want the rule to apply to.

  3. For the When box, specify a JSON expression that will evaluate to true when you want the action to be permitted.


    If you specify {}, the expression will always evaluate to true and MongoDB Realm will always allow the associated actions to be called.


Save the Configured Rule

Once you’ve finished configuring the rule, click Save. Once saved, the rule takes effect immediately.


Export Your Realm Application

To define a new service rule with realm-cli, you need a previously created application configuration.

You can export your application configuration from the Export tab of the Settings page in the Realm UI, or by running the following command from an authenticated instance of realm-cli:

realm-cli export --app-id=<App ID>

Create a Service Rule Directory

If it doesn’t already exist, create a new rules subdirectory in the /services folder of the exported directory.

mkdir -p services/<service name>/rules

Create and Configure a New Service Rule

Add a rule configuration JSON file to the rules directory. The configuraiton file should have the same name as the rule and have the following form:

  "name": <string>,
  "actions": [<action>, ...],
  "when": <JSON expression>
Configuration Value Description

Rule Name

Required. A name for the rule. The name should be unique within the service.



Required. An array of strings where each item is the name of a service action that the rule applies to.


An HTTP service rule that only applies to the service action would have the following action value:

"actions": [


Required. A JSON expression that determines if a service action should actually execute after being called. MongoDB Realm will only execute the action if the expression resolves to true. If you do not specify an expression, Can Evaluate defaults to true.

Import the Service Rule

Once you’ve configured the service rule, all that’s left is to import it into your Realm app.

  1. Ensure that <rule name>.json is saved.

  2. Navigate to the root of the exported application directory.

  3. Log in to MongoDB Atlas with realm-cli:

    realm-cli login --api-key="<my api key>" --private-api-key="<my private api key>"
  4. Import the directory:

    realm-cli import

Now that you have imported the rule, it takes effect immediately.