Generate data using the Mockaroo Generate API in Node.
npm install mockaroo
http://docs.mockaroo.com/api/node
The mockaroo client's generate method returns a promise that resolves to an array of records.
You can either generate data using a schema that you've built and saved on www.mockaroo.com:
const Mockaroo = require("mockaroo");
const client = new Mockaroo.Client({
apiKey: "(your api key)", // see http://mockaroo.com/api/docs to get your api key
});
const records = await client.generate({
count: 10,
schema: "My Saved Schema",
});
Or you can specify fields using the API:
const records = await client.generate({
count: 10,
fields: [
{
name: "id",
type: "Row Number",
},
{
name: "transactionType",
type: "Custom List",
values: ["credit", "debit"],
},
],
});
Field types and parameters are documented here
You can also download data in csv format:
const records = await client.generate({
count: 10,
format: 'csv',
header: true, // this is the default, set to false to remove the header row
fields: [...]
})
The Promise returned by client.generate resolves to an array of records when count > 1 and a single object when count == 1. The keys are the names of the fields in your schema. For example:
const records = await client.generate({
count: 10,
fields: [
{
name: "id",
type: "Row Number",
},
{
name: "transactionType",
type: "Custom List",
values: ["credit", "debit"],
},
],
});
records.forEach((record, i) => {
console.log(
`[${i}] id: ${record.id}, transactionType: ${record.transactionType}`
);
});
This module contains Error classes to help you handle specific error conditions.
try {
const records = await client.generate({
count: 10,
schema: "My Saved Schema",
});
} catch (error) {
if (error instanceof Mockaroo.errors.InvalidApiKeyError) {
console.log("invalid api key");
} else if (error instanceof Mockaroo.errors.UsageLimitExceededError) {
console.log("usage limit exceeded");
} else if (error instanceof Mockaroo.errors.ApiError) {
console.log("api error: " + error.message);
} else {
console.log("unknown error: " + error);
}
}
This module uses mocha and chai for testing. To run tests:
yarn test