Axway Appcelerator Blog

The Leading Resource for All Things Mobile

Using Faker for Mock Data

49 Flares 49 Flares ×

In a prior post, we discussed how to use mock data for your API. This post describes how to use faker in an API Builder Custom API to provide mock data for your application developers, so they can proceed with their work while you build out your backend and API infrastructure.

In this post, I will create an API, called user, that will take a parameter count for the number of users to return, and will return user name, email address, mobile phone, a photo and GPS coordinates for where the user works.

Faker

Faker is a powerful npm for generating mock data. You can read more about it here.

Faker can generate data in the following categories, each with many fields:

  • address
  • commerce
  • company
  • database
  • date
  • fake
  • finance
  • hacker
  • helpers
  • image
  • internet
  • name
  • phone
  • random
  • system

In my user example, I am using the the following faker fields in order to create user data:

  • name.firstName
  • name.lastName
  • phone.phoneNumber
  • internet.domainName
  • image.avatar
  • address.latitude
  • address.longitude

To use faker in your API Builder project simply install the npm:


npm install faker

and add faker to your package.json dependencies section:


.
.
"dependencies": {
    "async": "^1.5.0",
    "lodash": "^3.10.1",
    "pkginfo": "^0.3.1",
    "faker": "^4.1.0"
},
.
.

API Builder Custom API

We can add the faker mock data capability to a model via blocks but, in this example, I chose to implement a Custom API. You can read more about API Builder Custom APIs here.

My Custom API is shown below:


var Arrow = require('arrow');
var faker = require('faker');

var user = Arrow.API.extend({
	group: 'alexa',
	path: '/api/user',
	method: 'GET',
	description: 'api to request users',
	parameters: {
		count: {description:'number of users'}
	},
	action: function (req, resp, next) {
    faker.seed(123); // If you want consistent results
    var count = req.params.count;
    var responseArray = [];
    if(count >0) {
        for(var i=0; i<count; i++) {
          var firstName = faker.name.firstName();
          var lastName = faker.name.lastName();
          var username = firstName.charAt(0)+lastName;

          var responseObject = {
            fname: firstName,
            lname: lastName,
            email: username.toLowerCase()+'@'+faker.internet.domainName(),
            mobile: faker.phone.phoneNumber(),
            photo: faker.image.avatar(),
            lat: faker.address.latitude(),
            lon: faker.address.longitude()
          };
          responseArray.push(responseObject);
        }
    }
    resp.status('200');
    resp.send({"success": true, "data": responseArray});
    next();
	}
});
module.exports = user;

The Custom API above does the following:

  • Requires in the faker npm
  • Creates a GET API with an /api/user path
  • Requires a parameter count
  • Sets a seed for Faker so that each call will return the same data
  • Creates an array of users
  • Uses faker to get a first and last name and derives a username based on first and last name
  • Uses faker to create an email address, mobile phone number, photo, latitude and longitude
  • Returns the array of users in the JSON response

A sample call to this API is shown below:


http://localhost:8080/api/user?count=5

It returns the following:


{
  "success": true,
  "data": [
    {
      "fname": "Maurine",
      "lname": "Rath",
      "email": "mrath@edison.info",
      "mobile": "657-744-7946",
      "photo": "https://s3.amazonaws.com/uifaces/faces/twitter/conspirator/128.jpg",
      "lat": "-3.4323",
      "lon": "-129.6178"
    },
    {
      "fname": "Giuseppe",
      "lname": "Jacobs",
      "email": "gjacobs@everardo.name",
      "mobile": "1-342-063-5761 x4105",
      "photo": "https://s3.amazonaws.com/uifaces/faces/twitter/charlesrpratt/128.jpg",
      "lat": "5.7290",
      "lon": "-25.9692"
    },
    {
      "fname": "Lorenza",
      "lname": "Gulgowski",
      "email": "lgulgowski@roslyn.info",
      "mobile": "1-763-743-6332 x2296",
      "photo": "https://s3.amazonaws.com/uifaces/faces/twitter/andresenfredrik/128.jpg",
      "lat": "-73.4212",
      "lon": "99.7216"
    },
    {
      "fname": "Horacio",
      "lname": "Barrows",
      "email": "hbarrows@hipolito.biz",
      "mobile": "(140) 384-7869 x554",
      "photo": "https://s3.amazonaws.com/uifaces/faces/twitter/allagringaus/128.jpg",
      "lat": "12.3175",
      "lon": "-138.3774"
    },
    {
      "fname": "Elizabeth",
      "lname": "Halvorson",
      "email": "ehalvorson@jerry.info",
      "mobile": "(892) 144-9254 x6714",
      "photo": "https://s3.amazonaws.com/uifaces/faces/twitter/bcrad/128.jpg",
      "lat": "53.5911",
      "lon": "37.1017"
    }
  ]
}

Now that we have an API that returns mock data, the application developers can proceed with their application while we build out the production APIs.

49 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 49 Email -- 49 Flares ×

Sign up for our blog!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
49 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 49 Email -- 49 Flares ×