Platform

How to use CLI to develop and manage your backend

Introduction

The Back4App Command Line Interface (CLI) has many useful commands to work with your app. In this tutorial, you will learn the main commands available to create, manage, deploy and develop apps through Back4App.

At any time, you can access the complete Back4App CLI Project at our GitHub repository.

1 - Help

The help command is the most important command of the b4a interface. By typing b4a help in your terminal, all the commands available at Back4App CLI will be listed, as shown below:

$ b4a help
Parse Command Line Interface
Version 3.0.7
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.

By using b4a help <command_name> you get additional information about a particular command.

$ b4a help new
Adds Cloud Code to an existing Parse app, additional can create a new Parse app.
You can also use it in non-interactive mode by using the various flags available.

Usage:
  parse new [flags]

Flags:
 -a, --app string   Name of the Parse app you want to create or set up Cloud Code project for.
 -c, --create       Set this flag to true if you want to create a new Parse app.
 -h, --help         help for new
 -i, --init         Create a Cloud Code project only with configuration.
 -l, --loc string   Location, relative to the current directory, at which the Cloud Code project will be set up.
 -n, --nocode       Do not set up a Cloud Code project for the app. Typically used in conjunction with 'create' option

2 - Create

You can create a new Back4App app using the b4a new command. It will ask you some questions about the initial configuration and by the end of the process you will have a new Back4App app with the given name. Additionally, this sets up a Cloud Code Project for the app.

To test the command, just follow the instructions provided in the end of the output. The default workflow that you will come across will be like the one below, in which an app is created with the default options:

$ 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
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)
Successfully configured email for current project to: "your configured email"
Your Cloud Code has been created at /home/igor/back4app/docs/CLI-Back4App.

This includes a "Hello world" cloud function, so once you deploy,
you can test that it works, with the printed curl command.

Next, you might want to deploy this code with:

	cd CLI-Back4App
	b4a deploy

Once deployed you can test that it works by running:
curl -X POST \
 -H "X-Parse-Application-Id: vcn5AHKETyx9E8CYXZHGeYDKKuQvkHFXQWdMFNn6" \
 -H "X-Parse-REST-API-Key: uxu3XkpfpcsjgQAzmgouVGHOquPe8N8o73JMqkRG" \
 -H "Content-Type: application/json" \
 -d "{}" \
 https://parseapi.back4app.com/functions/hello

The created directory looks like this:

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.

3 - Manage your apps

After you created some apps, it is important to start managing them. At this step you will go through some commands that will help you succeed at this task.

3.1 - List

The command b4a list will show a list of all apps connected to your Back4App account. If this is executed inside a directory created by the b4a new, it will also list the apps that are associated with Cloud Code in the current directory. The output looks like the following:

$ 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

3.2 - 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"

3.3 - 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 in 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

4 - Deploy

To deploy the code from your machine to the Parse Cloud, you need to run b4a deploy. This pushes new code to the cloud (in cloud and public directories) and deploys it for the default target which is the first app that was added or the one you set using b4a default. You can choose to deploy a different target by adding the target as an argument to deploy, as shown below:

This command needs to be executed inside the project folder.

$ b4a deploy app_name
Uploading source files
Uploading recent changes to script…
The following files will be uploaded:
/users/user_name/app_name/cloud/main.js
Uploading recent changes to hosting…
The following files will be uploaded:
/users/user_name/app_name/public/asd.py
users/user_name/app_name/public/index.html
Finished uploading files
New release is named v1 (using Parse_JavaScript SDK v2.2.25)

You can add release notes to the deploy with the -d or --description.
If the contents of your Back4App Project remain unchanged, then the deploy is skipped. You will see an output like this:

$ b4a deploy
Uploading source files
Finished uploading files
Not creating a release because no files have changed

You can override this behavior with the -f or -force flag. Providing this flag forces a deploy despite no changes have been made to your project.

When embedding Parse deploy in other scripts, such as in an automated testing/deploy environment, you can rely on the exit code from the Parse command line tool to indicate whether the command succeeded. It will have an exit code of 0 on success and a non-zero exit code when the deploy failed.

5 - Develop

After your app is already deployed, you can enter the development mode by using the develop command, as shown below:

$ b4a develop
Did not provide app name as an argument to the command.
Please enter an app name to execute this command on
or press ENTER to use the default app "CLI-Back4App": (Enter)
Your changes are now live.
The server is listening on port 3000

This will make the tool not only watch the source directory for any updates and deploy them to Back4App, but also provide a live stream of Cloud Code logs.

It’s done!

You now have learned how to use the basic features of the Back4App Command Line Interface and are ready to start using the CLI in your own application.