Navigation

Stitch CLI

Overview

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

Installation

The easiest way to install stitch-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-stitch-cli

You can also install the CLI from npm with Yarn:

yarn global add mongodb-stitch-cli
1

Download the stitch-cli binary

You can download the stitch-cli binary directly from the Stitch UI.

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

Add the stitch-cli binary to your system PATH

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

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

mv /path/to/downloads/stitch-cli /usr/local/bin/stitch-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 stitch-cli.exe from the command line, your system searches each directory listed in your %PATH% environment variable for the stitch-cli executable file.

Move stitch-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 stitch-cli

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

chmod +x path/to/stitch-cli

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

ls -l | grep "stitch-cli"

The output of this command should resemble the following:

-rwxrwxr-x@  1 user  staff      9443152 Mar  6 12:01 stitch-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.

CLI Commands

General Options

stitch-cli

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

--config-path
Optional.

If included with stitch-cli login, stores information about the authenticated session in a file at the specified path. Session information includes your MongoDB Cloud username, 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.

--disable-color
Optional.

If specified, suppress all text color in stitch-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 stitch-cli.

Import an Application

stitch-cli import

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

stitch-cli import \
  --app-id=myStitchApp-abcde \
  --path=./path/to/app/dir \
  --strategy=merge
--app-id <MongoDB Stitch Application ID>
Optional.

The Application ID of your MongoDB Stitch application.

If not specified, stitch-cli will attempt to use the value of app_id defined in stitch.json. If stitch.json does not have an app_id value, stitch-cli will ask if you’d like to create a new application.

--path <path>
Optional.

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

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

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

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

--project-id
Optional.

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

Note

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

Export an Application

stitch-cli export

Use stitch-cli export to save a Stitch application configuration to a local application directory.

stitch-cli export \
  --app-id=myStitchApp-abcde \
  --output=path/to/exported/app/dir
--app-id <MongoDB Stitch Application ID>
Optional.

The Application ID of your MongoDB Stitch application.

--output <path>
Optional.

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

If specifiied, stitch-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, stitch-cli will export the application configuration to a new directory within the current working directory.

--as-template
Optional.

If enabled, stitch-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.

Authenticate a CLI User

stitch-cli login

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

stitch-cli login --api-key=my-api-key --private-api-key=my-private-api-key
--api-key <api key>

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

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

A valid MongoDB Atlas private 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

stitch-cli logout

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

stitch-cli logout

View the Currently Logged In User

stitch-cli whoami

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

stitch-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, stitch-cli will return the following message:

no user info available

Import Strategies

When performing an application import, there are two built-in strategies for handling existing entities: merge and replace.

All imports follow the merge strategy unless otherwise specified.

Merge

stitch-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. If an entity is imported with an id that does not match an existing entity, the import will fail. Entities without id values will be assigned system-generated id values before being imported as new entities.

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

stitch-cli import --strategy=replace

Under the replace strategy, any existing entities in an application are deleted if they are not represented in the imported application directory.

If an imported entity has no id value or has an id value that does not match an existing entity’s id, stitch-cli will assign it a system-generated id. Unlike in the merge strategy, importing an entity with a non-ObjectID id value will 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.