Mock Your API Responses with Random Key
Random Key releases a new API to help devs mock API calls
Mocking backend REST servers has become the way to go for many organisations to speed up development and testing. Random Key’s Mock Data API is the latest addition to the apps available on the market: what makes our API unique is its data-centric approach.
With Random Key’s Mock API you decide the structure of your responses and which items you wish to randomize. Your schema might include static elements that you wish to keep as they are, and attributes you want to generate with Random Key. Each item marked for randomization is given a custom name, the data type to be returned, and the number of instances to be generated. You can choose from locations, numbers, names, dates, regular expressions, and others.
Imagine you were querying a backend customer database and expected to receive ids, names, and addresses within a customer object. The data would then be used to populate a different store or displayed in an application. This is difficult to achieve with typical Mock APIs that focus on the response structure while not offering much (if any) variance in the returned data. With Random Key’s Mock API requesting an id, name, and address of a client is as simple as:
{
"customer": {
"@cust_id": {
"type": "number",
"digits": 5
},
"@name": {
"type": "name",
"portion": "full",
"gender": "0",
"region": "us"
},
"@address": {
"type": "location",
"region": "us"
}
}
}
The application will return a json response matching the requested schema and will fill in the requested attributes with randomized data:
{
"customer": {
"cust_id": "80628",
"name": "Terry Reed",
"address": [
"Greenfield",
"Wisconsin",
"53129"
],
}
}
Next time you run the same query, a different dataset is returned:
{
"customer": {
"cust_id": "07761",
"name": "Lilith Thao",
"address": [
"Honeyville",
"Utah",
"84302"
],
}
}
The response values can be set to arrays, carrying multiple items of the same type. Here’s an example of generating an array of departmental codes along with the customer record:
{
"customer": {
"@cust_id": {
"type": "number",
"digits": 5
},
"@name": {
"type": "name",
"portion": "full",
"gender": "0",
"region": "us"
},
"@address": {
"type": "location",
"region": "us"
},
"@dep_codes": {
"type": "regex",
"regex": "\\d\\d[A-Z]",
"records": 3
}
}
}
The following is sent back:
{
"customer": {
"cust_id": "56621",
"name": "Carsten Rupp",
"address": [
"St. Louis",
"Missouri",
"63103"
],
"dep_codes": [
"77R",
"18J",
"12P"
]
}
}
The data can be customized to follow any database schema. At the time of writing this article, developers can choose between Integer, Double, Number, Regex, Name, Email Address, Location, National Insurance Number, Social Security Number, Numéro de Sécurité Sociale, Internal Russian Passport, Credit Card Number, Date, Date & Time, Time services. The full list of all supported properties can be found in the Mock API documentation.
Here’s an example of a more elaborate REST API call that requests a customer ID, their full name, their billing address, their credit card number, their Social Security Number, the last purchase’s time and date, the starting date as a customer, and the total spending amount with the store.
{
"customer": {
"@cust_id": {
"type": "number",
"digits": 5
},
"@fname": {
"type": "name",
"portion": "first",
"gender": "0",
"region": "us"
},
"@lname": {
"type": "name",
"portion": "last",
"gender": "0",
"region": "us"
},
"@billing_address": {
"type": "location",
"region": "us"
},
"@ccn": {
"type": "ccn",
"portion": "number"
},
"@ssn": {
"type": "ssn"
},
"@last_purchase": {
"type": "datetime",
"format": "%b-%d-%Y %H:%M",
"min": "Jan-01-2019 00:00",
"max": "Jan-01-2020 00:00"
},
"@start_date": {
"type": "datetime",
"format": "%b-%d-%Y %H:%M",
"min": "Jan-01-2010 00:00",
"max": "Jan-01-2019 00:00"
},
"@total_spending": {
"type": "double",
"min": "10",
"max": "10000"
}
}
}
Every attribute to be randomized needs marked with an “@” sign (prepanded to its name — the sign is later removed). The configuration options require the type of data that is to be returned, and any relevant attribute specification, such as gender or region. See how in the example above the customer ID is referred to as @cust_id: that flags to Random Key that the item has been selected for randomization. The attribute is given a “number” type, which — as per API’s documentation — “produces a random number of the specified length”. The length is passed as the second element of the array: it’s 5 digits. The number is returned in a string format as the API might generate leading zeros. For other numeric attributes that require an integer or a double in return, an appropriate data type is chosen: see the example of @total_spending in the REST call. The function will generate a random double within the given range.
The same convention is used across other available data types. The Name function asks for the portion of a name (full, first, or last), the gender (female, male, or random), and the region, or in other words, the country the name typically occurs within. The region is particularly useful if we’re after a dataset covering various languages and character sets, like French or Cyrillic. Similarly, a region is required for generating location data. You can set it to any country supported by Random Key. It’s good to consult the documentation for regional differences: e.g. for many datasets, the surname is gender-neutral yet for Slavic countries, like Russia, the surname can be set to male or female.
Other noteworthy elements are the Social Security Number and the Credit Card Number functions: they don’t require any additional properties to be specified. The data returned follows the SSN and CCN specifications: this is particularly useful if part of the application tests is the format validity.
The following response is received:
{
"customer": {
"cust_id": "56621",
"fname": "Latoya",
"fname": "Kennedy",
"billing_address": [
"Houston",
"Texas",
"77396"
],
"ccn": "5189852041736493",
"ssn": "332233353",
"last_purchase": "Dec-11-2019 03:11",
"start_date": "Sep-05-2016 20:46",
"total_spending": "8496.89"
}
}
The key take away here is that Random Key allows for a lot of customization in your requests. You can reuse data elements (e.g. call the “date” function multiple times), assign your custom labels to the returned attributes, and play with various types of data. Multiple geographical regions are supported to target the tests to a specific market. Finally, Random Key does not force any front end client or software to install: to get started, you only need a REST client or CURL. Simply send your request to the Mock API’s endpoint and you’re ready to start testing! Import our API from Postman and start using Random Key within seconds, or refer to our documentation to set it up yourself.