How to Build and Test an Ionic SMS App

This video shows how to build and test an Ionic SMS app using Nexmo API. The video tutorial first shows how to create a backend to invoke Nexmo API. Then the Ionic app is created which uses the backend. The app is then tested in the browser and on the device using the Appery.io Tester app. The video also shows how to build a binary file for Android and iOS.

Looking for more videos to help build mobile apps? Check out the Appery.io YouTube channel.

Video: How to Build an Ionic App with an RSS Feed

This short video shows how to build an Ionic app that connects to an RSS feed. You can click on a link and navigate to the article. RSS feed used in this tutorial: http://feeds.gawker.com/lifehacker/full.

There are many more videos on the Appery.io YouTube channel to help you build mobile apps fast.

Learn How to Connect Your App to an API Backend

This short video shows how to import backend services APIs into your app. Appery.io services consist of a cloud database, Server Code for custom app logic, and API Express for enterprise integration and service orchestration.

Our YouTube channel has many more short videos where you can learn how to build apps fast with Appery.io.

Moving App Logic to the Server: How to Save and Query an Appery.io Database from Server Code Script

The Appery.io Database provides storage for your mobile app data. You can store data such as users, orders, purchases, products or anything else. The Appery.io Database is a cloud NoSQL database and provides a very simple and elegant REST API to work with the database.

One of the most common cases for an enterprise mobile app is to create/save a new item/record and then redisplay the list in the app with the newly created item.

Accessing the database directly from the client is fine but requires two requests to the database.

  1. A request to save the new item/record into the database.
  2. Once the 1st request completes, on its success, a request to get the updated list to display in the app.

A better approach is to do both the create and list in a single request from the app. This is very simple to do from Appery.io Server Code script. The following script shows how to create a new record and then get the updated list:

var dbId = "cdaec951....d8";
var collectionName = "People"

var newName = request.get("name");
var newTitle = request.get("title");

// Save new object into the collection
Collection.createObject(dbId, collectionName, {
   "name": newName,
   "title": newTitle
});

// Retrieve updated data from the collection
var result = Collection.query(dbId, collectionName);
// Return the updated list (including the new object)
Apperyio.response.success(result, "application/json");

In this simple script you first create a new Person object (line 8) and then do another query to get the updated list of people (line 14). The script response returns the updated list of people. This script automatically has a REST API which is the invoked from the app.

What’s most important, there is only a single request from the app to the Server Code script. A single request is usually better than two requests. Also, you can add additional logic to the script without impacting the app. For example, you can send an email when a new person is added. Or, you can send  Push Notification when a new person is added. All this can be done without impacting the app. That’s nice.

If you want to learn more about Server Code, check out our YouTube channel Server Code playlist.

Moving App Logic to the Server: How to Query the Database From Appery.io Server Code Script

An Appery.io developer posted a question on the Appery.io Facebook page on how to write a Server Code script to query two different collections in the database. This post will show you how to do that.

In general, querying two database collections is better to do on the server-side than from the client. Doing it on the server is a lot faster and provides you with the option to update the search without impacting the client. Plus, you can add any logic when needed. Doing it from the client would require two REST API calls to the database.

Let’s jump into the example.

First, there are two collections in the Appery.io Database. They look like this:

ProductName collection:

Screen Shot 2016-05-24 at 4.42.32 PM
ProductName collection.

This collection is simple. It has a name and a code.

This is the ProductData collection:

Screen Shot 2016-05-24 at 4.44.33 PM.png
ProductData collection.

The code in this collection links the product name to the actual product (Android or iOS device).

The input to the script is the product type: Android or iOS. Based on the input, you lookup the code for the product (a or i) and then run a second query to find all iOS or Android phones in the ProductData collection. Here is the script.

var product = request.get("type");
var dbId = "367e9e77......";
var params = {};
var result = [];

// Search for object with the provided 'type' (iOS or Android)
params.criteria = {
  "name": {
    "$eq": product
  }
};
result = Collection.query(dbId, "ProductName", params);

// Find all objects with code 'i' or 'a'
params.criteria = {
  "code": {
    "$eq": result[0].code // Either 'i' or 'a'
  }
};
// Run the query
result = Collection.query(dbId, "ProductData", params);
// Script response (API response)
Apperyio.response.success(result, "application/json");

This script first searches the ProductName collection to find the id for iOS or Android devices. Then it searches the ProductData collection to find the actual devices.

Moving this logic to the server is a good idea as you query two collections (or even more) with a single request from the client.

If you want to learn more about Server Code, check out our YouTube channel Server Code playlist.

Integrating With PayPal API for Mobile Payments

PayPal is obviously one of the most popular services and APIs to use when you want to support payments in you mobile app. This tutorial will show you how to create a single payment using Appery.io Server Code. You can the use it as a starting point for all other APIs made available by PayPal.

If you read the PayPal documentation, the first step is to get a token so you can invoke PayPal APIs. Before you can get the token, you need to create an app in PayPal:

Screen Shot 2016-04-27 at 4.25.01 PM
Apps in PayPal

Every app has a Client ID and Secret values that you need to use to get a token.

Screen Shot 2016-04-27 at 4.31.49 PM
Client ID and Secret

The Server Code script below sends a request to PayPal to get a token:

var url = "https://api.sandbox.paypal.com/v1/oauth2/token";

var XHRResponse = XHR2.send("POST", url, {
  "parameters": {
    "grant_type": "client_credentials"
  },
  "headers": {
    "Accept": "application/json",
    "Accept-Language": "en_US",
    "Authorization": "Basic clientid:secret_as_base64"
});

Apperyio.response.success(XHRResponse.body, "application/json");

The response looks like this:

{
 "app_id": "APP-80W2.....",
 "access_token": "A101.VyA8xBdNyOY.....",
 "token_type": "Bearer",
 "scope": "https://uri.paypal.com/....",
 "nonce": "2016-04-27T23:15:21Zahq3.....",
 "expires_in": 31284
}

The access_token is what you need to invoke any other PayPal API.

Now that you have the token, you can invoke any PayPal API. In the next script I create a payment (charge):

var url = "https://api.sandbox.paypal.com/v1/payments/payment";

var XHRResponse = XHR2.send("POST", url, {
 "headers": {
    "Content-type": "application/json",
    "Authorization": "Bearer A101.VyA8xBdNyOY....."
 },
 "body": {
    "intent": "sale",
    "payer": {
       "payment_method": "credit_card",
       "funding_instruments": [{
          "credit_card": {
             "number": "4060xxxxxxxxxxxx",
             "type": "visa",
             "expire_month": 06,
             "expire_year": 2019,
             "cvv2": "874",
             "first_name": "Betsy",
             "last_name": "Buyer",
             "billing_address": {
                "line1": "111 First Street",
                "city": "Saratoga",
                "state": "CA",
                "postal_code": "95070",
                "country_code": "US"
             }
          }
       }]
    },
    "transactions": [{
       "amount": {
          "total": "7.47",
          "currency": "USD",
          "details": {
             "subtotal": "7.41",
             "tax": "0.03",
             "shipping": "0.03"
          }
       },
       "description": "This is the payment transaction description."
       }]
    }
});

Apperyio.response.success(XHRResponse.body, "application/json");

The payment example was adopted from PayPal API documentation

One important thing you need to do is use a valid credit card number from your test account. Without a valid credit card number, the request will not work. To find your credit card number go to Sandbox > Accounts, expand your account and click Profile. Select the Funding tab and use the credit card and expiration data from there for the test payment.

Once you have a working Server Code script that creates a payment in PayPal, you can use the script in your mobile app. A Server Code script is automatically exposed via a REST API which you can call from your app. We have many videos on our YouTube channel that show how to use Server Code and other Appery.io backend services.

How to Do Multi-Object Update in MongoDB From Appery.io Server Code

The Appery.io Database runs on MongoDB database. The following screen shot shows a sample collection (table) with employees in the database.

Screen Shot 2016-04-19 at 5.01.31 PM.png
Custom database collection

You want to update two or more objects in a single request. That’s very simple to do from Appery.io Server Code. The following server script does multi-object update, it just one line of code:

var result = Collection.multiUpdateObject("367e9e77-cc1f-4c1f...",
    "People", '{"title":"Engineer"}', {"title":"Senior Engineer"});
  • The first argument is the database API key.
  • People is the collection name inside the database.
  • With {“title”:”Engineer”} you are searching for all records where the title is set to “Engineer” and update those objects to a title of “Senior Engineer“.
  • Collection is part of Server Code API that integrates with the database.

That’s it.

After you run the script, the database collection looks like this:

Screen Shot 2016-04-20 at 11.35.22 AM
Collection after the update

Appery.io Server Code provides a very nice integration with the Database that allows you to run and execute advanced app logic.

Learn more about Appery.io Database and Server Code from many short videos on our YouTube channel.

How to Add Geocoding Lookup in Your App in 5 Minutes

Let’s say you are building a mobile app where you enter an address and need to get back the location as latitude/longitude information. To do this in Appery.io Server Code is super simple. Here is a Server Code script that looks up an address and returns the latitude/longitude information for the location using the Google Geocoding API:

var address = request.get("address");
var url = "https://maps.googleapis.com/maps/api/geocode/json"; 

var XHRResponse = XHR2.send("GET", url, {
   "parameters": {
      "address": address,
      "key": "AIzaSyAFQBtqmC.........."
    }
});

var responseInJson = JSON.parse(XHRResponse.body);

Apperyio.response.success(responseInJson.results[0].geometry.location, "application/json");

When you run this script, the result looks like this (using Boston as input):

{
   "lng": -71.0588801,
   "lat": 42.3600825
}

The script has an API which you can invoke from your app:

https://api.appery.io/rest/1/code/540cb503-f9a7-4dd1-8926-af959383e2b2/exec?address=Boston

Here is an example invoking the script directly from the browser:

Screen Shot 2016-04-13 at 12.34.54 PM
Invoking the Server Code script

With geolocation logic on the server, you can change the implementation — for example use a different API without making any changes to an app and impacting the users.

Want to learn more? Check out the large collection of videos we have on our YouTube channel.

What is Appery.io Server Code?

Server Code is one of the core components in the Appery.io platform and allows to write custom app logic. You write the app logic inside a server-side script which is executed on the server. The language used to write the script is JavaScript.

Server Code uses V8 JavaScript engine to execute the JavaScript on the server.

During development the Server Code script can be quickly tested inside editor.

When you are done writing the script, the script has a REST API URL which can be invoked from your app. In other words, a script is also a way to create a custom REST API.

You have access to pretty much all JavaScript objects. You also have built-in API that allows you to work and integrate with other Appery.io components as well as external systems. This is the built-in API list:

  • Console – used to print data for information or debugging purposes.
  • Collection – provides integration with the Appery.io database.
  • Database user – provides integration with the Users collection from Appery.io database.
  • Push  Notifications – used to send Push Notifications messages.
  • Request – holds request (input) information for the script.
  • Response – used as a response (answer) for the request.
  • ScriptCall – used to call another script.
  • XMLHttpRequest – used to send HTTP requests to web server and retrieve any type of data directly back into the script.
  • In Memory Data (IMD) – allows to store any data in fast memory for 20 minutes. It works like a very fast cache.

In addition to writing scripts, you can also create libraries. A library can include logic that can be included and re-used in other scripts.

Some examples using Server Code:

  • Run any custom logic/calculations.
  • Get data from the Appery.io database, process/format the data that makes sense for the app.
  • Send a targeted Push Notification message.
  • Check the database and if some condition is met (for example: low inventory), send a Push Notification message and email to the appropriate people.
  • Invoke an external REST API, process/format the response and return a result that make sense for the app.
  • Invoke two external REST APIs, combine the data in a format that makes sense for the app.
  • Write a script that registers and signs in a user from the app.

As mentioned at the beginning, a script is invoked via its REST API. You can also schedule a script to run periodically. It’s called a Job. For example, you can schedule a script to run once a day.

A script can be made secure which means a user login will be required before a script can be invoked. A user is created inside the Appery.io database Users collection.

With build-in versioning, a script version can be saved and restored at any point.

Server Code snippets and plug-ins provide sample code that can be quickly inserted into any script.

We have a number of short videos on using the Server Code on our YouTube channel here and here.

Happy coding.