Mock JSON API

Create a mock JSON API in minutes ✈️

Get Started Creating a Mock JSON API

Using the editor below you can quickly create a Mock JSON API. Simply copy and paste the JSON that you want to access over HTTP into the field, choose a method and status code and press Create API.

If you need multiple endpoints and more features such as test data generation, simulated failures and simulated delays you can use our tool Mocki. It let's your create fully fledged mock JSON APIs using code or a user friendly graphical interface.

After creating the JSON API you can access it over HTTP from any origin.

Use Cases for a Mock JSON API

There are many use cases for a mock JSON API and here we have explained a few of them. You probably ended up on this page with a particular use case in mind but here you will get some inspiration on how to extend your usage.

Testing Independent of External Services

When running GUI tests with a real backend in place there might be many prerequisites to set up before running a test. You probably need a user to sign in with, a running backend instance in the correct environment and so on. If you use a mock API instead of your real backend you can run your tests without having to worry about everything behind the frontend. Using a tool such as Mocki you can also simulate errors and delays in the mock API to see how your app handles them. This will help you speed up your development flow and deliver higher quality products.

A mock API can also be useful when testing APIs themselves. A common case is that your API or service needs to call external services over HTTP that it integrates to. If you want to get rid of these dependencies when running tests you can use mock APIs to your advantage. By mocking the external dependencies you do not need to put unnecessary load on the external systems and can be free of having to make sure they are constantly up and running.

Develop Independent of a Backend

Sometimes you might want to start working on a feature before there is a backend service available to support it. Using a mock JSON API you can start developing towards the mock API during the time that the real service is not available. This enables you to work quicker and also help you give feedback to the backend developers on the data that you need in the frontend. Once the real backend is finished you can switch over to that and your frontend should work just as before.

Generating Test Data

Sometimes it can be difficult to get the data that you want from the backend. For example it might have to be added manually which is a tedious process. To fix this and test the frontend or other systems with more varied test data you can use a mock API with test data generation such as Mocki to create the data that you need.

Learning How to Interact With APIs

If you are in the beginning of your career as a software developers a mock API can help you learn how to interact with APIs. In this case using a mock API is much easier than using a commerical API provider. Usually you have to sign up and read up on their documentation before starting to make requests. Once you have learned how to interact with a mock API you can move on and make real integrations to build cool products.

Using your API

Let's take a look at how you can use your API to access the data in a couple of different programming languages. In the example we have used the JSON below, use it or roll your own.

{
    "users": [
        {
            "name": "Jake",
            "age": 22
        },
        {
            "name": "Maria",
            "age": 45
        },
        {
            "name": "Charlie",
            "age": 12
        },
        {
            "name": "Kim",
            "age": 85
        }
    ]
}

To jump directly to the language you are using click the links below:

In Python

In Python you can use the requests library to make an HTTP call to your mock JSON API. In the below example we use it to fetch and display some data from the API. We are using a simple GET request here but the library has support for all types of requests.


# importing the requests library
import requests

# api-endpoint
URL = "https://api.mocki.io/k39cs7x"

# sending a get request
r = requests.get(url = URL)

# extracting data in json format
data = r.json()

# print the first users name in the console

print(data["users"][0].name)

In JavaScript

To call our JSON API in JavaScript we can use the built in fetch library. Since it is built into the browser there is no need to import it. It is supported by the major browsers so should be safe to use for most cases. Here we are using a GET request just as in the previous example.

fetch('https://api.mocki.io/v1/xyz123') // make the http request
    .then(response => response.json()) // parse the json into a js object
    .then(json => console.log(json.users[0].name)) // log the first users name to the console

In PHP

The simplest way to make a Mock JSON API call in PHP is to use the curl library. This comes built into PHP so you won't have to import anything just like in JavaScript. If you want a more clean way of doing API calls you can use something like the Guzzle client.

// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://api.mocki.io/xyz123");
// $output contains the output json
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// here we print the entire output
var_dump(json_decode($output, true));

In Go

Using Golang we can utilize the built in http package. Read more about the different options that you can use in the official documentation.

// make the request to our api
resp, err := http.Get("https://api.mocki.io/xyz1234")

// close to body to avoid resource leaks
defer resp.Body.Close()

// get the body from the response
body, err := ioutil.ReadAll(resp.Body)

// log the result
log.PrintLn(string(body))

More languages?

If you want us to add your favorite language make sure to contact us using the chat symbol in the corner and we will try to add it. Happy hacking!