Navigation

Realm CLI

Overview

realm-cli

The MongoDB Realm Command Line Interface (realm-cli) allows you to programmatically manage your Realm apps. With realm-cli, you can create or update Realm apps from a local directory as well as export existing applications to a local directory.

Installation

The easiest way to install realm-cli is with the npm package manager. Ensure that you have Node.js installed, then run the following command in your shell:

npm install -g mongodb-realm-cli

You can also install the CLI from npm with Yarn:

yarn global add mongodb-realm-cli
1

Download the realm-cli binary

You can download the realm-cli binary directly from the Realm UI.

  1. Navigate to the Deploy page from the left-hand navigation.
  2. Select the Import/Export tab.
  3. In the Download CLI Client section, select the operating system on which you want to install realm-cli. The CLI binary should begin downloading immediately.
../../_images/download-cli-realm-ui.png
2

Add the realm-cli binary to your system PATH

When you call realm-cli from the command line, your shell searches each directory listed in your PATH environment variable for the realm-cli executable binary.

Move the realm-cli binary file from your downloads folder to a directory listed in your PATH.

mv /path/to/downloads/realm-cli /usr/local/bin/realm-cli

You can check which directories are listed in your PATH by running the following from the command line:

echo $PATH

To add a directory to your path, add the following to the bottom of your shell rc file (e.g. ~/.bashrc), substituting in the path of the directory you’d like to add:

export PATH=${PATH}:path/to/directory

When you call realm-cli.exe from the command line, your system searches each directory listed in your %PATH% environment variable for the realm-cli executable file.

Move realm-cli.exe to a directory that is listed in %PATH%.

You can check which directories are listed in %PATH% as well as add new directories from the Environment Variables configuration window. You can find this window by searching for “Environment Variables” or by following these steps:

  1. Depending on which version of Windows you’re running, right click My Computer or This PC and select Properties.
  2. Click Advanced Properties, then Environment Variables.

In the Environment Variables window, your PATH variable is listed under System Variables.

3

(Linux / macOS) Configure execution permissions for realm-cli

On Linux and Mac systems, no user has execution permissions for realm-cli by default. To add execution permissions, run the following command:

chmod +x path/to/realm-cli

To confirm that realm-cli has execution permissions, navigate to the directory containing the binary and run the following command:

ls -l | grep "realm-cli"

The output of this command should resemble the following:

-rwxrwxr-x@  1 user  staff      9443152 Mar  6 12:01 realm-cli

The ls -l command lists the file’s permissions followed by some additional information. If the permissions string in your output includes the letter x then you have successfully configured execution permissions. If no x is present (e.g. -rw-r--r--@) you did not successfully configure execution permissions and should run the chmod command as described above.

General Options

The following options are available for all realm-cli commands:

--config-path <File System Path>
Optional.

If included with realm-cli login, stores information about the authenticated session in a file at the specified path. Session information includes your MongoDB Cloud username, MongoDB Atlas programmatic API Key, and a session refresh token.

If included with any other command, authenticates the request with the session saved at the specified path (if it exists) instead of the current CLI authentication state.

Warning

Since the session configuration file contains your MongoDB Atlas programmatic API private key, you should avoid sharing this file unintentionally.

--disable-color
Optional.

If specified, suppress all text color in realm-cli output.

By default, some output such as errors and import diffs are colorized. Use this flag if you wish to prevent this behavior.

--yes
Optional.

If specified, automatically respond affirmatively to any yes/no prompts from realm-cli.

Authentication

Authenticate a CLI User

realm-cli login

Use realm-cli login to authenticate a MongoDB Cloud user with a MongoDB Atlas programmatic API Key.

realm-cli login --api-key="<my api key>" --private-api-key="<my private api key>"
--api-key <api key>

A valid public MongoDB Atlas programmatic API key for the MongoDB Cloud account you wish to log in with.

--private-api-key <private api key>

A valid private MongoDB Atlas programmatic API key for the MongoDB Cloud account you wish to log in with.

--username <MongoDB Cloud username>

(Deprecated) The username of the MongoDB Cloud account you wish to log in with using personal API keys.

Log Out the Current CLI User

realm-cli logout

Use realm-cli logout to log out the currently logged in user.

realm-cli logout

View the Currently Logged In User

realm-cli whoami

Use realm-cli whoami to see details on the user that is currently logged in to the CLI, if applicable.

realm-cli whoami

If there is a currently logged-in user, their information will display on the next line in the following format:

<username> [API Key: ********-****-****-****-<last 12 digits of API key>]

If no user is logged in, realm-cli will return the following message:

no user info available

Realm Applications

Import an Application

realm-cli import

Use realm-cli import to import a local application directory into a hosted Realm app. If you import a directory into an application that doesn’t exist, realm-cli can create the application for you.

realm-cli import \
  --app-id=myapp-abcde \
  --path=./path/to/app/dir \
  --strategy=merge \
  --include-hosting \
  --include-dependencies
--app-id <MongoDB Realm Application ID>
Optional.

The Application ID of your Realm app.

If not specified, realm-cli will attempt to use the value of app_id defined in realm.json. If realm.json does not have an app_id value, realm-cli prompts you to create a new application.

New Application App IDs

If you create a new application with realm-cli, MongoDB Realm generates a new App ID and ignores any value that you specify for the --app-id flag.

--path <path>
Optional.

The path to the directory containing files you wish to import. The directory must contain, at minimum, a valid realm.json file.

If the path argument is omitted, realm-cli will search for a realm.json file in the current application directory.

--strategy ['merge|replace|replace-by-name']
Optional.
Default: Merge

The import strategy that realm-cli should use when reconciling imported entities.

--project-id <MongoDB Cloud Project ID>
Optional.

The Project ID of the Atlas project on which you want to host a newly created Realm app. If specified, realm-cli will not prompt you to select a project when creating a new app.

Note

realm-cli ignores the value of --project-id unless you are importing a new application.

--include-hosting
Optional.

If specified, uploads and deploys any static assets in the /hosting/files directory of your Realm app.

--include-dependencies
Optional.

If specified, uploads and deploys any external dependencies included in a node_modules archive in the /functions directory of your Realm app.

Export an Application

realm-cli export

Use realm-cli export to save a Realm app configuration to a local application directory.

realm-cli export \
  --app-id=myRealmApp-abcde \
  --output=path/to/exported/app/dir \
  --include-hosting \
  --as-template
--app-id <MongoDB Realm Application ID>
Optional.

The Application ID of your Realm app.

--output <path>
Optional.

The path of the directory where Realm will export your application.

If specified, realm-cli creates a directory at the given path and exports the application configuration into the new directory. If a file or directory already exists at the specified path, the export will fail.

Note

If the output argument is omitted, realm-cli will export the application configuration to a new directory within the current working directory.

--include-hosting
Optional.

If specified, exports all hosted static assets in the hosting/files directory of your Realm app.

--for-source-control
Optional.

If enabled, realm-cli exports the application configuration without any fields that conflict with deployment via GitHub source control, including fields like name, app_id, location, and deployment_model in the realm.json file as well as the config.clusterName field in the config.json of any Atlas clusters linked to the application.

--as-template
Optional.

If enabled, realm-cli exports the application configuration without any service ID values, including the App ID. This simplifies the creation of new applications from an exported configuration.

Diff Pending Application Changes

realm-cli diff

Use realm-cli diff to return a diff of the configuration files between the deployed application and your local application directory.

# Diff application config files
realm-cli diff

# Diff application config files and hosted files
realm-cli diff --include-hosting

The diff resembles the following:

--- functions/oldFunctionName/config.json
+++ functions/oldFunctionName/config.json
@@ -1,6 +1 @@
-{
-    "id": "5d4c6a5cd28e555496a705da",
-    "name": "oldFunctionName",
-    "private": false
-}

--- functions/newFunctionName/config.json
+++ functions/newFunctionName/config.json
@@ -1 +1,6 @@
+{
+    "id": "5d4c6a5cd28e555496a705da",
+    "name": "newFunctionName",
+    "private": false
+}

Modified Files:
        * /index.html
        * /auth/confirmEmail.html
        * /auth/resetPassword.html
--include-hosting
Optional.

If specified, the diff includes a list of files in the hosting/files directory of your Realm app that are different from your app’s deployed files.

Secrets

List All Secrets

realm-cli secrets list

Use realm-cli secrets list to return a list that contains the Name and ID of each Secret in your application.

realm-cli secrets list

The returned list of secrets resembles the following:

ID                       Name
5d5c25415e30c7ef857c6a10 test-secret-please-ignore
5d56dd453b467e2a48a6ec32 some-other-secret

Create a Secret

realm-cli secrets add

Use realm-cli secrets add to create a new Secret with the specified name and value.

realm-cli secrets add --name=mySecret --value=SuperSecretValue!
--name <Secret Name>

A unique name for the new Secret. If an existing Secret already has the specified name then this operation will fail.

--value <Secret Value>

The value of the new Secret.

Update a Secret’s Value

realm-cli secrets update

Use realm-cli secrets update to change the value of an existing Secret in your application.

# Update a Secret by name
realm-cli secrets update --secret-name=mySecret --value=NewSecretValue
realm-cli secrets update --name=mySecret --value=NewSecretValue

# Update a Secret by name
realm-cli secrets update --secret-id=5ba9c5c2e707c02b38031412 --value=NewSecretValue
realm-cli secrets update --id=5ba9c5c2e707c02b38031412 --value=NewSecretValue
--secret-name <Secret Name>
--name <Secret Name>

The name of the Secret to update.

--secret-id <Secret ID>
--id <Secret ID>

The ID value of the Secret to update.

--value <Secret Value>

The new value of the Secret.

Remove a Secret

realm-cli secrets remove

Use realm-cli secrets remove to delete an existing Secret from your application.

# Remove a Secret by name
realm-cli secrets remove --secret-name=mySecret
realm-cli secrets remove --name=mySecret

# Remove a Secret by ID
realm-cli secrets remove --secret-id=5ba9c5c2e707c02b38031412
realm-cli secrets remove --id=5ba9c5c2e707c02b38031412
--secret-name <Secret Name>
--name <Secret Name>

The name of the Secret to remove from your Realm app.

--secret-id <Secret ID>
--id <Secret ID>

The ID value of the Secret to remove from your Realm app.

Import Strategies

When performing an application import, there are multiple built-in strategies for handling existing entities.

All imports default to the merge strategy unless otherwise specified.

Merge

realm-cli import --strategy=merge

Under the merge strategy, entities in the application directory are added to the application non-destructively. Any existing entities in an application are left unchanged if they are not represented in the imported application directory.

If an imported entity’s id value matches the id of an existing entity, the existing entity will be updated to match the imported entity. Realm assigns system-generated id values to entities without id values before importing them as new entities.

If an entity is imported with an id that does not match an existing entity, the import will fail. Importing an entity with a non-ObjectID id value causes an error.

Note

If an imported entity has an id field, the value must be an ObjectID or the merge will fail.

Example

An existing application has three functions:

{ "id": <ObjectID 1>, "name": "FunctionA", ... }
{ "id": <ObjectID 2>, "name": "FunctionB", ... }
{ "id": <ObjectID 3>, "name": "FunctionC", ... }
A local application directory is imported with the merge strategy.
The directory contains configuration files for the following functions:
{ "id": <ObjectID 1>, "name": "FunctionA_Updated!", ... }
{ "name": "FunctionD", ... }

After importing, the application has the following functions:

{ "id": <ObjectID 1>, "name": "FunctionA_Updated!" }
{ "id": <ObjectID 2>, "name": "FunctionB", ... }
{ "id": <ObjectID 3>, "name": "FunctionC", ... }
{ "id": <ObjectID 4>, "name": "FunctionD", ... }

FunctionA was updated based on its imported configuration file. FunctionB and FunctionC were not included in the imported application directory, so they remained unchanged after importing with the merge strategy. FunctionD was imported as a new entity and assigned a system-generated id value.

Replace

realm-cli import --strategy=replace

Under the replace strategy, if an imported entity’s id value matches the id of an existing entity, Realm replaces the existing entity with the imported entity. If an imported entity’s id value does not match an existing entity, the import fails. If an existing entity’s id does not match the id of any imported entity, Realm deletes that existing entity.

Realm generates id values for entities that lack id values before importing them as new entities. Importing an entity with a non-ObjectID id value does not throw an error.

Example

An existing application has three functions:

{ "id": <ObjectID 1>, "name": "FunctionA", ... }
{ "id": <ObjectID 2>, "name": "FunctionB", ... }
{ "id": <ObjectID 3>, "name": "FunctionC", ... }
A local application directory is imported with the replace strategy.
The directory contains configuration files for the following functions:
{ "id": <ObjectID 1>, "name": "FunctionA_Updated!", ... }
{ "name": "FunctionD", ... }
{ "id": "non-ObjectID-value", "name": "FunctionE", ... }

After importing, the application has the following functions:

{ "id": <ObjectID 1>, "name": "FunctionA_Updated!" }
{ "id": <ObjectID 4>, "name": "FunctionD", ... }
{ "id": <ObjectID 5>, "name": "FunctionE", ... }

FunctionA was updated based on its imported configuration file. FunctionB and FunctionC were not included in the imported application directory, so they are not present in the app after importing with the replace strategy. FunctionD and FunctionE were imported as new entities and assigned system-generated id values.

Replace by Name

realm-cli import --strategy=replace-by-name

Under the replace-by-name strategy, if an imported entity’s name value matches the name of an existing entity, Realm replaces the existing entity with the imported entity. If an imported entity’s name value does not match an existing entity, the entity becomes a new entity. If an existing entity’s name does not match the name of any imported entity, Realm deletes that existing entity.

If an imported entity has no name value, realm-cli will throw an error.

Example

An existing application has three functions:

{ "id": <ObjectID 1>, "name": "FunctionA", ... }
{ "id": <ObjectID 2>, "name": "FunctionB", ... }
{ "id": <ObjectID 3>, "name": "FunctionC", ... }
A local application directory is imported with the replace strategy.
The directory contains configuration files for the following functions:
{ "name": "FunctionZ", ... }
{ "name": "FunctionB", ... }
{ "name": "FunctionC", ... }

After importing, the application has the following functions:

{ "id": <ObjectID 2>, "name": "FunctionB", ... }
{ "id": <ObjectID 3>, "name": "FunctionC", ... }
{ "id": <ObjectID 4>, "name": "FunctionZ", ... }

Both the existing application and the imported configuration directory contained functions with the names FunctionB and FunctionC. As a result, both functions retained their previous id values and names. The rest of both function’s values reflect the values uploaded from the configuration files. FunctionA was not included in the imported application directory, so it is not present in the app after importing with the replace-by-name strategy. FunctionZ was imported as a new entity and assigned a system-generated id value.