DEV Community

Cover image for Fake API - An API to generate fake data
Amal Shaji
Amal Shaji

Posted on • Edited on • Originally published at blog.amalshaji.com

Fake API - An API to generate fake data

Here is an API for all the frontend developers. An API to generate fake data, lots of it. This tool comes in handy when you are developing a frontend for applications that requires data a lot of data.

Frontend frameworks like React, Vue, etc., are trendy, and many applications are built on these frameworks. Hot reloading is one of the features of such libraries. So when you press save, the whole app reloads, and all the APIs are called.

Making multiple API calls every few seconds is a costly task. Keeping the backend running the whole time to prototype the frontend is also an expensive process. This is where an API generating fake data comes into play.

API Base URL

How does the API work?

The API is built using FastAPI, and the fake data is generated using the Faker library. All the data types(the type of data generated) supported by the API are the ones in Faker.

Endpoints

  • [GET] /api/types

This endpoint returns all the data types supported. Only these types should be provided as values to any key.

  • [POST] /api/

This endpoint does all the heavy lifting. It generates fake data based on the JSON payload. Each key in the payload is an identifier, i.e., the key in the resulting fake data. The value to each key must be one of the valid data types. For an invalid type, the API ignores the entry.

For example,

{
    "name": "name"
}
Enter fullscreen mode Exit fullscreen mode

Will generate something like,

{
    "name": "Amal Shaji"
}
Enter fullscreen mode Exit fullscreen mode

What kind of payload can the API handle?

  • Simple Payloads

simple.PNG

  • Complex payloads (nested JSON)

complex1.PNG

  • Complex payloads (Maybe the person has multiple contact information)

complex2.PNG

You can use the repeat key for repeating any block of data. Max repeat count is 10.
Use the repeat key in the outer block to get data for n users.

The function to generate data based on the payload is recursive. So whenever the function encounters a dict, it calls itself, solving all the nested structures. You can read about it in the source code.

interesting.PNG

Hmm. Interesting...

Navigate to https://fakeapi.tk/api/types to know about all the supported data types. Fire up your favorite API testing client and test it out with various data types.

If you find any bugs, feel free to open an issue or create a pull request.

Happy Hunting🥳🥳

Top comments (1)

Collapse
 
sdomingobasora profile image
Sergi Domingo

Nice idea.