Using Appery Backend APIs in an App

Connecting to API backend from the app.

Introduction

Every mobile app has a backend or a server which it connects to. The app uses APIs to connect to this backend to view, edit, and store data. The Appery.io platform provides a complete backend or MBaaS (mobile backend as a service) for your app.

This page explains how to create and connect to an API backend, including 3rd party REST APIs.

Backend Services

The Appery.io platform provides the following backend services for your app:

  • API Express;
  • Server Code;
  • Database;
  • Push Notifications.

Every service listed above is exposed via REST APIs and that's how the app connects/talks to the backend.

Importing API Services

As all these services are part of a unified platform, you can quickly import any of these services inside the App Builder. To import a service inside the App Builder, select CREATE NEW, you need to select the service you want to import.

🚧

Adding Push Notifications

Push Notifications are added to the app another way - via the EVENTS panel (for JQM and Ionic 4) or by using snippets (for Ionic 1).

You can import REST API services into your app easily regardless of what backend service you use: API Express, Server Code, or Database.
matter what app you are working with, to start importing a backend service, click Create new and then select the service type you want to import under Backend Services.

Backend Services import menu.Backend Services import menu.

Backend Services import menu.

Watch this short video to learn how to import Backend Services APIs:

Testing Services

When importing the service double-check that the service was imported correctly. As the service is imported automatically you shouldn't need to make any changes. But it's always a good idea to test the service before importing it into the app. You should get the same output testing the corresponding service as you get when testing the app in work.

Editing Services

If you need to modify the service, you can make changes to the service in the App Builder or remove and re-import the services. The general recommendation is to re-import the service (after deleting them). This way you can go back to the backend services (API Express, Server Code, or Database), make any changes to the service, and then re-import it. However, be careful with this option as any mappings you set for this service will be removed as well.

❗️

Deleting Service Used on a Page

If you remove a service used on the page, all its mappings will be removed as well.

Using Imported Service on Page

Once a service is imported, you can add it and use it from a page. To learn how to do this, jump over to the App Builder section:

The following sections cover each service type in more detail.

Importing API Express Services (API Express projects)

Once you created one or more APIs using API Express, you can quickly and easily import (add) those services to your app.

👍

Testing services before using them in an app

Before importing and using an API Express service in your app, we recommend that you test it in the API Express dashboard. Once you know the service works correctly then start using the service in your app. This approach will save you time debugging and testing your app.

To import API Express services:

  1. Inside the App Builder, click Create new.
  2. In the Backend Services section, select API Express Service.
  3. In the next popup, select the API Express project from which you want to add services to.
  4. Once a project is selected you should see the same folder/service structure as in the API Express project. Select one or more services or a folder to import. When you select a folder all services inside the folder will be checked and imported.
  5. Once you selected what you need, click the Import selected services button.
  6. The services that you selected will be imported under the Services folder inside the App Builder.

You can now test the service.

Importing Auto-generated API Services

If you auto-generated API services for a relational database you need the API Express Generator Extension to import the services into an app.

The API Express Generator extension is an extension that allows you to generate multiple REST API services for a database table. You can quickly generate the following REST APIs to work with the database:

  • Create - to add a new record;
  • Find - to find one or more records;
  • Get - to retrieve a record;
  • Update - to update an object;
  • Delete - to delete a record.

With this extension, the services and JavaScript files are created automatically and can be used in the app as usual to perform CRUD operations.

  1. Open an app, then click Create New > Extensions > API Express Generator.
  2. You will see a domain selection screen. Keep the default Appery.io domain selected and click Next.
  3. On the next screen, select the API Express project and click Select.
  4. On the next screen, you will see the services inside this API Express project. Locate the service folder which you want to import and click the generate link for it:
  1. Enter a name under which the services will be imported in the App Builder or keep the default value and click Generate.
  2. Click Save in the App Builder and refresh the App Builder browser page to see the newly generated services in the Services folder.

All the basic operations services, as well as the JavaScript, have been successfully generated.
The API Express Generator has created an AppClientSettings service file with the data on the domain where the project is located and its apiKey.

Another way to locate the project API key is to go to API Express > Project > Settings.

🚧

Editing API Express Services

If you edit the services in API Express, you should regenerate the appropriate services inside the Appery.io App Builder.

Importing Server Code Services

Once you created one or more Server Code scripts, you can quickly and easily import (add) those services to your app.

👍

Testing services before using them in an app

Before importing and using a Server Code service in your app, we recommend you test the Server Code script. Once you make sure the service works correctly you can securely add the service to your app. This approach will save you time debugging and testing your app.

To import a Server Code service:

  1. Inside the App Builder, click Create new.
  2. In the Backend Services section, select Server Code.
  3. In the next popup, select the Server Code script you want to import the service for.
  4. Once you select the script, click the Import selected services button.
  5. The service that you selected will be imported under the Services folder inside the App Builder:

You can now test the service.

📘

If you are new to Server Code, this "Creating Your First App Backend" tutorial will help you start.

Importing Database Services

Once you created a database, you can quickly and easily import (add) services to work with the database to your app.

To import a database service:

  1. Inside the App Builder, click Create new.
  2. In the Backend Services section, select Database Service.
  3. In the next popup, select the database for which you want to import the services.
  4. Once you select the services, click the Import selected services button. (Check the next section for the explanation on the various services).
  5. The service you selected will be imported under the Services folder inside the App Builder:

You can now test the service.

Database Service Types

In the import window, there are two groups of services you can import.

In the Database user services section, you can import the services to work with the Users collection:

  • Login;
  • Logout;
  • Signup;
  • Me;
  • Update User;
  • Delete User;
  • User Queries;
  • User Details;
  • All Users.

In the Database collection services section, you can import the services for any custom collection and for the Files collection (_files).

For a custom collection, you can import any of the following services:

  • Create - to create a new object;
  • Read - to read one object;
  • Update - to update an object;
  • Delete - to delete an object;
  • List - to list all objects in the collection;
  • Query - to query (search) for objects;
  • Distinct - to retrieve all distinct values;
  • Batch Update - to perform batch update.

For the Files collection, you can import any of the following services:

  • Create - to create a file;
  • Upload - to upload a file;
  • Read - to get the file (data);
  • Delete - to remove the file;
  • List - to retrieve a files list.

Using Database in App

The Appery.io Database is exposed via REST APIs, which you can import and then use in an app as described in the section above. Connecting to the database directly from the app works okay for very simple or small apps. For example, if your app only needs to load the entire list of objects and display them on a page.

If your app does more than just this (and that's most apps), we recommend using the following approach. Create a Server Code script that will access the database. This allows writing any logic in the script which you don't have to do on the client. For example, you can:

  • Sort the database results;
  • Filter the database results;
  • Query the database two or more times;
  • Run any custom logic before or after querying the database.

We recommend this as invoking app logic is better on the server. This approach allows you to write as simple or as complicated logic using server-side JavaScript. Once you developed the script, it's very simple to test it. Once the script is tested, use it in your app. If you need to make changes to the script, you can make changes without impacting the app.

You can check our detailed tutorial on working with Appery.io Database and Server Code here: https://docs.appery.io/docs/working-with-apperyio-database-and-server-code.
Here are two more examples that demonstrate the benefits of using Server Code to access the database:

In addition to accessing the database, you can also send Push Notifications and invoke any 3rd party API from the Server Code script. For more details, please check this post.