Local Development

Command Line Interface Setup

20min

Command Line Interface Setup

Introduction

The Command Line Interface (CLI) is a tool that allows you to interact with the back4app platform via a terminal or command window. You can use CLI to:

  • Create a new app;
  • Deploy local files (e.g. Cloud Functions files);
  • Manage app releases;
  • Set the SDK version;
  • Deploy a web application;
  • Install NPM modules via package.json

In this guide you will create a simple back4app app using the CLI and deploy your local Cloud Functions files.

Goal

  • To install and setup CLI in your terminal;
  • To use CLI to create a new app at Back4App;
  • To use CLI to connect to an existing app;
  • To use CLI to deploy Cloud Code Functions and files.

For information about Cloud Code Function, please click here.

Prerequisites

To complete this tutorial, you will need:

  • A Back4App account.

Configuring Command Line Interface

To manage your apps at Back4App, you need to install the Command Line Interface in your operational system.

Select the appropriate tab to start the installation:

1 - Install Back4App CLI

MacOS and Linux

Windows

Step 1 - Install Back4App CLI

The Command Line Interface for Windows is available here. You need to download the b4a.exe file.

Document image
ο»Ώ

Then you need to copy and paste that executable to C:\windows\system32, so that b4a becomes executable by the command line.

Note: this is not an installer, it is just a plain Windows executable.

2 - Connect CLI with your back4app account

To connect your account with CLI you need to get the back4app account key. To obtain your account key, log into your account at the back4app website and hover over the Hello, < username > button (top right).

Account Keys

Account keys are personal access tokens that can be used to create a new Back4App app, and list properties and keys for the given Back4App app.

Click on Account Keys:

Document image
ο»Ώ

You can find existing keys or create a new one. All you need to do is to write your key name (1) and click on β€˜+’ (2) to save this to your key list.

Document image
ο»Ώ

Go to your terminal and type b4a configure accountkey. Paste the key you created previously and press Enter.

$ b4a configure accountkey Input your account key or press ENTER to generate a new one. NOTE: on pressing ENTER we'll try to open the url: "http://dashboard.back4app.com/classic#/wizard/account-key" in default browser. Account Key: ${ACCOUNT_KEY} Successfully stored account key for: "your configured email". $

This command will only work for the first time you do this setup on a specific computer. Otherwise, run b4a configure accountkey -d to overwrite your old key with a new one.

The account keys you’ve used are stored in ${HOME}/.back4app/netrc for Mac and Linux.

If your account is not properly connected using an account key, you’ll need to log in with your Back4App credentials every time you run a deploy command. To avoid this situation run this step again. If this does not work check this file to make sure you are using the account key you selected in your Back4App account.

3 - Create a new App

To create a new app:

  1. Open your terminal and type the command: b4a newο»Ώ
  2. Type (n) and choose the app name. Please see the commands printed on your terminal below:
$ b4a new Would you like to create a new app, or add Cloud Code to an existing app? Type "(n)ew" or "(e)xisting": n Please choose a name for your Parse app. Note that this name will appear on the Back4App website, but it does not have to be the same as your mobile app's public name. Name: CLI-Back4Appο»Ώ

4 - Connect to an existing App

  1. Open your terminal and type the next command: b4a new.
  2. Type (e) and select the number for the name of the application you want to configure:
$ b4a new Would you like to create a new app, or add Cloud Code to an existing app? Type "(n)ew" or "(e)xisting": e 1: CLI-Back4App 2: Live Query 3: MyApp2 Select an App to add to config: 3 Please enter the name of the folder where we can download the latest deployed Cloud Code for your app "MyApp2" Directory Name: (Enter)ο»Ώ

5 - Deploy a Cloud Function

Let’s now create a Cloud Function on your local environment and then deploy it to your back4app connected app. First, we need to create a local cloud code file called main.js and then deploy it using the CLI.

Cloud Code Function

  • The Cloud Code function is a tool that allows you to run a javascript function on back4app servers. Back4app executes your code once you call the function via GraphQL,ο»ΏRESTorSDK.

Go back to your CLI and then press ENTER to automatically create your local folder structure.

Awesome! Now it's time to set up some Cloud Code for the app: "CLI-Back4App", Next we will create a directory to hold your Cloud Code. Please enter the name to use for this directory, or hit ENTER to use "CLI-Back4App" as the directory name. Directory Name: (Enter) You can either set up a blank project or create a sample Cloud Code project Please type "(b)lank" if you wish to setup a blank project, otherwise press ENTER: (Enter)ο»Ώ

To check your file structure go to your computer file system and navigate to /home/{USER}/{Name-App}. CLI has created 2 local folders. The first folder cloud contains your cloud functions files and was created with an initial main.js file. The second folder public will have your web application files. The final structure is shown below.

main.js
Directory structure
ο»Ώ

Where .parse.local and .parse.project are config files that store application info and project-level info, respectively. The cloud directory stores your Cloud Code. Also in that directory, you’ll typically be editing main.js, which stores all of your Cloud Code functions. The public directory stores any static content that you want to host on Parse.

To deploy your cloud and public folders to your connected back4app app hit the command b4a deploy.

$ b4a deploy Uploading source files Uploading recent changes to scripts... The following files will be uploaded: /Users/{USER}/CLI-Back4app/cloud/main.js Uploading recent changes to hosting... The following files will be uploaded: /Users/{USER}/CLI-Back4app/public/index.html Finished uploading files New release is named v1 (using Parse JavaScript SDK v2.2.25)

ο»Ώ

You have now a cloud function deployed and ready to be used. To check your functions go to your App Dashboard and click on the Cloud Code Functions menu inside the Core section.

Document image
ο»Ώ

To call your functions go to our Cloud Code Functions guide and choose the way that best fits your needs.

CLI full commands list

To better understand CLI’s full potential you can run the help command on your terminal. It lists all the commands available at Back4App CLI (as shown below):

$ b4a help Parse Command Line Interface Version 3.1 Copyright 2018 Parse, Inc. http://parse.com Usage: parse [flags] parse [command] Available Commands: add Adds a new Parse App to config in current Cloud Code directory cloud Call cloud code function configure Configure various Parse settings default Sets or gets the default Parse App deploy Deploys a Parse App develop Monitors for changes to code and deploys, also tails parse logs download Downloads the Cloud Code project functions List Cloud Code functions and function webhooks generate Generates a sample express app in the current project directory help Help about any command jssdk Sets the Parse JavaScript SDK version to use in Cloud Code list Lists properties of given Parse app and Parse apps associated with given project logs Prints out recent log messages migrate Migrate project config format to preferred format new Adds Cloud Code to an existing Parse app, additional can create a new Parse app releases Gets the releases for a Parse App rollback Rolls back the version for the given app symbols Uploads symbol files triggers List Cloud Code triggers and trigger webhooks update Updates this tool to the latest version version Gets the Command Line Tools version Flags: -h, --help help for parse Use "parse [command] --help" for more information about a command.

After you create some apps, it is important to start managing them. The next tabs, include some commands that help you succeed at this task:

List

The commandb4a list shows a list of all the apps connected to your Back4App account. If this command is executed inside a directory created by the b4a new, it will also list the apps associated with Cloud Code in the current directory. The output appears as follows:

$ b4a list The following apps are associated with Cloud Code in the current directory: * CLI-Back4App These are the apps you currently have access to: 1: Back4AppTest 2: B4A 3: B4AApp 4: CLI-Back4App

Add

You can add a new Back4App app to config the current Cloud Code directory by simply running:

$ b4a add 1: Back4AppTest 2: B4A 3: B4AApp 4: CLI-Back4App Select an App to add to config: 2 Written config for "B4A"

Default

By using the b4a add command, you will have some apps running in the same directory, which may cause hassles when using other commands, like having to put the name of the app at the end of the command (like this b4a deploy < appname >). Fortunately, our CLI stores the default app to avoid this problem. To change the default app, simply type:

$ b4a default CLI-Back4App Default set to CLI-Back4App $ b4a default Current default app is CLI-Back4App

What to do next

As a next step, we recommend you to install and explore the back4app local environment back4app local environment (Local Parse Server).

Conclusion

Congratulations, now you can manage your applications via the terminal!