Generate Fake Data For Mock API Response
Imagine that you want to prototype an app with realistic-looking data, but the backend APIs are not there yet. That's where Beeceptor comes in. With Beeceptor, you have access to a powerful template engine that uses Handlebars' syntax. You can easily generate fake data that looks and feels realistic, without the hassle of dealing with messy code.
Fake data helpers
Beeceptor uses popular Faker's comparable syntax. It becomes a breeze to generate small/large data using a mocked response template. You can use pretty much all the attributes from Faker namespaces.
Enabling faker template
By default, template engine is off. You need to explicitly mark a rule to enable usage of Handlebars template to generate fake data.
The faker
Syntax
Beeceptor defines faker
as a Handlebars template helper.
Faker attributes are grouped into namespaces. E.g. datatype
, address
, company
, etc. To generate faked value for given attribute, prefix the attribute with its namespace. Refer below examples and values generated.
Syntax: {{faker 'namespace.attribute'}}
Sample Syntax | Generated value |
---|---|
{{faker 'random.uuid'}} | 52375efe-cf9e-44f5-adb2-65c1631bfcea |
{{faker 'address.country'}} | Spain |
{{faker 'finance.currencyCode'}} | USD |
{{faker 'hacker.phrase'}} | The AI hard drive is down, index the 1080p panel so we can override the COM firewall! |
{{faker 'lorem.paragraph'}} | Omnis perspiciatis provident error. Est possimus voluptates. Illo nam et excepturi blanditiis quae rerum et amet accusantium. Mollitia temporibus temporibus ea fugiat quia in possimus et. Ut provident est debitis perspiciatis ipsa non mollitia magnam quis. Et quod dolorum et non |
Refer the section below to view complete list of supported attributes.
Example using mocking template
You can mix and match any of the Faker attributes to build a rich response. Consider the following example for GET /users
API or model.
Mocking Template: A mocking template to build user objects.
[
{
"id" : "{{faker 'random.uuid'}}",
"name": "{{faker 'name.firstName'}} {{faker 'name.lastName'}}" ,
"address": "{{faker 'address.streetAddress'}}",
"country": "{{faker 'address.country'}}",
"phone": "{{faker 'phone.phoneNumber'}}",
"profileImage": "{{faker 'image.image'}}",
},
{
"id" : "{{faker 'random.uuid'}}",
"name": "{{faker 'name.firstName'}} {{faker 'name.lastName'}}" ,
"address": "{{faker 'address.streetAddress'}}",
"country": "{{faker 'address.country'}}",
"phone": "{{faker 'phone.phoneNumber'}}",
"profileImage": "{{faker 'image.image'}}",
}
]
Response: This will generate below output:
[
{
"id" : "cbe55c5d-36fa-4f05-9557-fcd4966187f4",
"name": "Dillon Davis" ,
"address": "613 Dickinson Run",
"country": "Tanzania",
"phone": "886.282.3509 x0831",
"profileImage": "http://placeimg.com/640/480/business",
},
{
"id" : "36df51a8-8d4c-4d70-8818-d77b9f6bf215",
"name": "Dannie Hackett" ,
"address": "8449 Brenna Alley",
"country": "Central African Republic",
"phone": "(786) 895-4923",
"profileImage": "http://placeimg.com/640/480/business",
}
]
Advance use-cases - Parameterized examples
You can pass arguments to the faker
syntax and generate even more specific and tailored fake data. Here are a few examples:
- Generate a date between given two dates:
{{faker 'date.between' '2022-05-01' '2022-05-15'}}
- Generate a past date in the last 5 years:
{{faker 'date.past' '5'}}
- Generate a female first name:
{{faker 'name.firstName' 'female'}}
- Generate a 10 digit alphanumeric
{{faker 'random.alphaNumeric' '10' }}
- Generate ten lines of lorem
{{faker 'lorem.lines' '10' }}
- Generate a paragraph
{{faker 'lorem.paragraph' }}
, or a paragraph with five sentences{{faker 'lorem.paragraph' '5' }}
- Generate a file name
{{faker 'system.fileName'}}
- Generates a random ethereum Address
{{faker 'finance.ethereumAddress'}}
- Generate a short product name
{{faker 'commerce.productName'}}
- Generate a random password
{{faker 'internet.password'}}
- Generate a random number between
{{faker 'mersenne.rand' '500' '1000'}}
- Generate a random domain name
{{faker 'internet.domainName'}}
You can checkout Fake JSON API as a reference when building the template. This is a pre-built mock server provided by Beeceptor for most common use-cases.
Supported attributes
Datatype: To generate random datatype values, string, uuid, etc.
datatype.number datatype.float datatype.uuid datatype.boolean datatype.string datatype.hexaDecimal
Random: To generate some random combinations.
random.word random.words random.image random.locale random.alphaNumeric
Lorem: Some blah blah... text and paragraphs.
lorem.word lorem.words lorem.sentence lorem.slug lorem.sentences lorem.paragraph lorem.paragraphs lorem.text lorem.lines
Person Names: Generate first/last name, titles, job type, etc.
name.firstName name.lastName name.findName name.jobTitle name.prefix name.suffix name.title name.jobDescriptor name.jobArea name.jobType
Address
address.zipCode address.city address.cityPrefix address.citySuffix address.streetName address.streetAddress address.streetSuffix address.streetPrefix address.secondaryAddress address.county address.country address.countryCode address.state address.stateAbbr address.latitude address.longitude
Phone Numbers:
phone.phoneNumber phone.phoneNumberFormat phone.phoneFormats
E-commerce: Some realistic product names.
commerce.color commerce.department commerce.productName commerce.price commerce.productAdjective commerce.productMaterial commerce.product
Company:
company.suffixes company.companyName company.companySuffix company.catchPhrase company.bs company.catchPhraseAdjective company.catchPhraseDescriptor company.catchPhraseNoun company.bsAdjective company.bsBuzz company.bsNoun
Finance:
finance.account finance.accountName finance.mask finance.amount finance.transactionType finance.currencyCode finance.currencyName finance.currencySymbol finance.bitcoinAddress finance.iban finance.bic
Date:
date.past date.future date.between date.recent date.month date.weekday
Online Forum:
hacker.abbreviation hacker.adjective hacker.noun hacker.verb hacker.ingverb hacker.phrase
Images: Generates links to images for the following categories.
E.g.
The image.animals
syntax will generate a link to http://placeimg.com/640/480/animals
. The image.image
syntax will generate a link to a random image for any of the below categories.
image.image image.avatar image.imageUrl image.abstract image.animals image.business image.cats image.city image.food image.nightlife image.fashion image.people image.nature image.sports image.technics image.transport image.dataUri
Internet:
internet.avatar internet.email internet.exampleEmail internet.userName internet.protocol internet.url internet.domainName internet.domainSuffix internet.domainWord internet.ip internet.ipv6 internet.userAgent internet.color internet.mac internet.password
Database:
database.column database.type database.collation database.engine
Git:
git.branch git.commitEntry git.commitMessage git.commitSha git.shortSha
System:
system.fileName system.commonFileName system.mimeType system.commonFileType system.commonFileExt system.fileType system.fileExt system.directoryPath system.filePath system.semver
Troubleshooting
When there are syntax errors in defining templates, Beeceptor tries to recover safely and attempts to generate the mocked response. However, there can be unrecoverable errors.
- If the
faker
syntax has any issue, you shall getFaker Attribute Error: Missing or wrong syntax
in the response. - If there is an unrecoverable error, a response with
561
HTTP status code is returned. In such cases, you should review the template's syntax and correct it.