What is an API?

When you think of an API, you typically think of a web endpoint that your application can request information from or send information to. The API endpoint for RandomAPI is http://beta.randomapi.com/api. You provide your API Key and API Ref ID and have the results that are generated from the API you queried for returned to you.


In the RandomAPI world, an API can be thought of as the Javascript source code that instructs the generator how to generate the random data you want.

const faker = require('faker');

api.user     = faker.name.findName();  
api.userID   = random.special(4, 5);  
api.password = random.special(3, 8);  
  "user": "Mireille Homenick DVM",
  "userID": "10057",
  "password": "Vq8EqJr0"

A RandomAPI API is an API (say that 3x fast) in the sense that you can provide input through the GET variables you send in your URI (?mode=1&list=23) and receive output that is affected by those inputs. But other than that, it is completely different from your run of the mill API.

With that definition out of the way, let's get into some technical details regarding APIs.

Every API has an implicit API object appropriately named api. All of the data that you want returned to you when you call your API must be attached to this object as a property. In this example, only the variable a will be returned since it is attached to the api object.

// Local variables. If you want to do complicated operations before attaching the result to the api object, local variables can be helpful

let num1 = 4;  
let num2 = 20;  
let a = num1 * num2;  
let b = 5;

// Attach a to the api object.
api.a = a;  
  "a": 80

You can also redefine the API object by assigning properties directly if you wish.

api = {  
  a: 5,
  b: 6
  "a": 5,
  "b": 6

You can even set the api object to something other than an object.

api = `Billy is ${random.numeric(13,21)} years old and likes the color ${list(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])}`  
"Billy is 17 years old and likes the color green"