Pythonium

Python, What else ?

MockAPY

Mock API

A free and open-source fake API designed for testing and prototyping, with Python for added flexibility.

Custom API & Rule Engine
Instantly generate a custom API from a JSON object and customize its behavior with a Python rule engine. Define rules to handle errors, modify HTTP responses, headers, and bodies using Python scripts—all for free and open source. Access the source code to set up your own local instance.

How to Create a Custom API?
MockAPY, available on GitHub, lets you create a local mock API with custom rules. Alternatively, you can generate an API online using the editors below, but without custom rules. The API will be immediately accessible but is deleted once a day. If needed, you can recreate it on this page. For frequent usage, consider setting up a local API to avoid limitations.

Resources




Documentation

Routes

GET/productsReturns all products
GET/products/2Returns the product with ID 2
POST/productsCreate a new product
GET/products/2Returns the product with ID 2
PUT/products/2Update the product with ID 2
PATCH/products/2Update partially the product with ID 2
DELETE/products/2Delete the product with ID 2

Configuration of the resources

All resources must be contained within a single JSON object, where each attribute represents a resource holding an array of elements.

In this example, two resources have been created: "products" and "users", each containing two elements.

{
  "products": [
    {
      "id": "1", "name": "T-shirt", "price": 19.99
    },
    {
      "id": "2", "name": "Jeans", "price": 49.99
    }
  ],
  "users": [
    {
      "id": "1", "username": "johndoe", "email": "johndoe@example.com"
    },
    {
      "id": "2", "username": "janedoe", "email": "janedoe@example.com"
    }
  ]
}

Configuration of the rules

Custom rules are defined using Python code, allowing you to set conditions based on the request (e.g., HTTP method, headers, and payload) and control the response (including HTTP status code and payload). This enables you to customize the API's behavior for specific testing and development needs.

Rules are executed before the API processes data (reading, writing, or deleting).

The HTTP status and payload specified in the rules override those defined by the API.

Here is an example of a rule that returns an HTTP status 401 when accessing the resource /products/23:

import request
import response
import json

def handle_request():
    if request.pathname() == "/products/23":
        response.status(401)
        response.exit()

The exit method indicates whether to halt after applying the rule or to continue with the API execution.

Input

The input object is used to represent the different criteria that can be tested in the request.

AttributeDescription
headersThe headers (Object: each header is an attribute)
methodThe HTTP method (‘GET’, ‘POST’, ‘PUT’, ‘PATCH’, ‘DELETE’)
pathThe pathname of the URL (e.g., /users/23).
payloadThe payload (object or string)

Output

The output object is used to update the response if the rule match.

AttributeDescription
statusSets the HTTP status (e.g., 200, 404, etc.)
responseSets the response payload
headersSets the headers (Object: each header is an attribute)