api-mock-ai 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- api_mock_ai-0.1.0/.gitignore +17 -0
- api_mock_ai-0.1.0/CHANGELOG.md +37 -0
- api_mock_ai-0.1.0/LICENSE +21 -0
- api_mock_ai-0.1.0/PKG-INFO +476 -0
- api_mock_ai-0.1.0/README.md +429 -0
- api_mock_ai-0.1.0/docs/ai-generation.md +79 -0
- api_mock_ai-0.1.0/docs/behaviour.md +163 -0
- api_mock_ai-0.1.0/docs/cli.md +69 -0
- api_mock_ai-0.1.0/docs/data-packs.md +86 -0
- api_mock_ai-0.1.0/docs/documentation-page.md +49 -0
- api_mock_ai-0.1.0/docs/getting-started.md +84 -0
- api_mock_ai-0.1.0/docs/how-it-works.md +121 -0
- api_mock_ai-0.1.0/docs/images/docs-page.jpg +0 -0
- api_mock_ai-0.1.0/docs/index.md +39 -0
- api_mock_ai-0.1.0/docs/python-api.md +74 -0
- api_mock_ai-0.1.0/docs/roadmap.md +25 -0
- api_mock_ai-0.1.0/docs/testing.md +85 -0
- api_mock_ai-0.1.0/examples/banking.yaml +446 -0
- api_mock_ai-0.1.0/examples/ecommerce.yaml +315 -0
- api_mock_ai-0.1.0/examples/petstore.yaml +135 -0
- api_mock_ai-0.1.0/pyproject.toml +98 -0
- api_mock_ai-0.1.0/src/api_mock_ai/__init__.py +8 -0
- api_mock_ai-0.1.0/src/api_mock_ai/__main__.py +4 -0
- api_mock_ai-0.1.0/src/api_mock_ai/chaos.py +84 -0
- api_mock_ai-0.1.0/src/api_mock_ai/cli.py +303 -0
- api_mock_ai-0.1.0/src/api_mock_ai/config.py +52 -0
- api_mock_ai-0.1.0/src/api_mock_ai/docs.py +198 -0
- api_mock_ai-0.1.0/src/api_mock_ai/errors.py +13 -0
- api_mock_ai-0.1.0/src/api_mock_ai/generate/__init__.py +292 -0
- api_mock_ai-0.1.0/src/api_mock_ai/generate/cache.py +47 -0
- api_mock_ai-0.1.0/src/api_mock_ai/generate/faker_gen.py +726 -0
- api_mock_ai-0.1.0/src/api_mock_ai/generate/llm_gen.py +184 -0
- api_mock_ai-0.1.0/src/api_mock_ai/generate/locales.py +380 -0
- api_mock_ai-0.1.0/src/api_mock_ai/py.typed +0 -0
- api_mock_ai-0.1.0/src/api_mock_ai/server.py +494 -0
- api_mock_ai-0.1.0/src/api_mock_ai/spec/__init__.py +6 -0
- api_mock_ai-0.1.0/src/api_mock_ai/spec/loader.py +111 -0
- api_mock_ai-0.1.0/src/api_mock_ai/spec/model.py +472 -0
- api_mock_ai-0.1.0/src/api_mock_ai/spec/schema.py +42 -0
- api_mock_ai-0.1.0/src/api_mock_ai/static/docs.html +669 -0
- api_mock_ai-0.1.0/src/api_mock_ai/store.py +178 -0
- api_mock_ai-0.1.0/src/api_mock_ai/validation.py +58 -0
- api_mock_ai-0.1.0/tests/__init__.py +0 -0
- api_mock_ai-0.1.0/tests/conftest.py +35 -0
- api_mock_ai-0.1.0/tests/test_cli.py +70 -0
- api_mock_ai-0.1.0/tests/test_docs.py +82 -0
- api_mock_ai-0.1.0/tests/test_generator.py +200 -0
- api_mock_ai-0.1.0/tests/test_llm.py +126 -0
- api_mock_ai-0.1.0/tests/test_loader.py +96 -0
- api_mock_ai-0.1.0/tests/test_locales.py +111 -0
- api_mock_ai-0.1.0/tests/test_model.py +131 -0
- api_mock_ai-0.1.0/tests/test_server.py +248 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here.
|
|
4
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project
|
|
5
|
+
uses [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.0] - 2026-12-01
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- `api-mock-ai serve`, `generate` and `validate` commands.
|
|
14
|
+
- OpenAPI 3.0 and 3.1 support (YAML/JSON, local files and URLs) with local `$ref` resolution,
|
|
15
|
+
including circular references.
|
|
16
|
+
- Automatic detection of REST resources, nested resources and id fields.
|
|
17
|
+
- Stateful in-memory CRUD with cascade delete and `POST /__mock__/reset`.
|
|
18
|
+
- Realistic Faker data based on formats, field names and the resource being generated,
|
|
19
|
+
with consistent people per object.
|
|
20
|
+
- Relations: nested resources, foreign keys (also nested), embedded objects.
|
|
21
|
+
- Optional LLM generation through LiteLLM with schema validation, Faker repair, fallback
|
|
22
|
+
and disk caching.
|
|
23
|
+
- Filtering, search, sorting and pagination, including envelope responses.
|
|
24
|
+
- Request body validation with readable `422` errors.
|
|
25
|
+
- Latency and error simulation (`--latency`, `--error-rate`, `X-Mock-Delay`, `X-Mock-Status`).
|
|
26
|
+
- Interactive documentation page at `/docs` (replaces Swagger UI): endpoints by tag,
|
|
27
|
+
schema trees, live examples, "Try it" console with error simulation and cURL export,
|
|
28
|
+
search, deep links, dark mode, mobile layout, offline.
|
|
29
|
+
- Country packs and regions for `--locale`: `pk`, `in`, `us`, `uk`, euro-area countries,
|
|
30
|
+
`eu` and `south_asia`, with local phones, cities, currency, price scale and banks.
|
|
31
|
+
- Banking data: bank names, IBAN with valid check digits, BIC, IFSC, account numbers,
|
|
32
|
+
routing numbers, sort codes, cards, PAN/CNIC/UPI, transactions with matching narrations.
|
|
33
|
+
- `examples/banking.yaml`, a retail banking API.
|
|
34
|
+
- Server-side fields on create (fields the request can't contain), order totals computed
|
|
35
|
+
from line items, several children per parent for nested resources.
|
|
36
|
+
- CORS enabled by default; clear error when the port is already in use.
|
|
37
|
+
- Python API: `create_app()` and `MockConfig`.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Muhammad Kashif
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: api-mock-ai
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Realistic, stateful mock APIs from any OpenAPI spec, powered by Faker or LLMs.
|
|
5
|
+
Project-URL: Homepage, https://github.com/kashy96/api-mock-ai
|
|
6
|
+
Project-URL: Documentation, https://kashy96.github.io/api-mock-ai
|
|
7
|
+
Project-URL: Repository, https://github.com/kashy96/api-mock-ai
|
|
8
|
+
Project-URL: Issues, https://github.com/kashy96/api-mock-ai/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/kashy96/api-mock-ai/blob/main/CHANGELOG.md
|
|
10
|
+
Author-email: Muhammad Kashif <engrmuhammadkashif96@gmail.com>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: api,faker,fastapi,frontend,llm,mock,mock-server,openapi,swagger,synthetic-data,testing
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Classifier: Framework :: FastAPI
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
25
|
+
Classifier: Topic :: Software Development :: Testing
|
|
26
|
+
Classifier: Topic :: Software Development :: Testing :: Mocking
|
|
27
|
+
Classifier: Typing :: Typed
|
|
28
|
+
Requires-Python: >=3.10
|
|
29
|
+
Requires-Dist: faker>=24.0
|
|
30
|
+
Requires-Dist: fastapi>=0.110
|
|
31
|
+
Requires-Dist: httpx>=0.27
|
|
32
|
+
Requires-Dist: jsonschema>=4.21
|
|
33
|
+
Requires-Dist: pyyaml>=6.0
|
|
34
|
+
Requires-Dist: rich>=13.0
|
|
35
|
+
Requires-Dist: typer>=0.12
|
|
36
|
+
Requires-Dist: uvicorn>=0.29
|
|
37
|
+
Provides-Extra: ai
|
|
38
|
+
Requires-Dist: litellm>=1.40; extra == 'ai'
|
|
39
|
+
Provides-Extra: dev
|
|
40
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
41
|
+
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
|
|
42
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
43
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
44
|
+
Provides-Extra: docs
|
|
45
|
+
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
|
|
46
|
+
Description-Content-Type: text/markdown
|
|
47
|
+
|
|
48
|
+
# api-mock-ai
|
|
49
|
+
|
|
50
|
+
**Realistic, stateful mock APIs from any OpenAPI spec, powered by Faker or LLMs.**
|
|
51
|
+
|
|
52
|
+
[](https://github.com/kashy96/api-mock-ai/actions/workflows/ci.yml)
|
|
53
|
+
[](https://pypi.org/project/api-mock-ai/)
|
|
54
|
+
[](https://pypi.org/project/api-mock-ai/)
|
|
55
|
+
[](https://github.com/kashy96/api-mock-ai/blob/main/LICENSE)
|
|
56
|
+
|
|
57
|
+
Frontend developers often wait for the backend. Mock servers help, but most of them
|
|
58
|
+
return placeholder data like `"name": "string"` and forget everything between requests.
|
|
59
|
+
|
|
60
|
+
`api-mock-ai` reads your OpenAPI spec and starts a mock server that:
|
|
61
|
+
|
|
62
|
+
- returns **realistic data**: real-looking names, emails, prices, addresses and dates,
|
|
63
|
+
chosen from field names and formats
|
|
64
|
+
- is **stateful**: `POST` creates a record, `GET` returns it, `DELETE` removes it
|
|
65
|
+
- keeps **relations consistent**: `GET /users/42/orders` only returns user 42's orders,
|
|
66
|
+
and `order.items[].productId` points to products that exist
|
|
67
|
+
- can generate data with **any LLM** (OpenAI, Anthropic, Gemini, or a local model through
|
|
68
|
+
Ollama), cached so the model is called only once
|
|
69
|
+
- speaks your users' **country**: `--locale pk`, `in`, `eu`, `uk`, `us`... with local phone
|
|
70
|
+
formats, cities, currency, price levels and **banking data** (banks, IBAN, BIC, IFSC)
|
|
71
|
+
- serves **interactive API documentation** at `/docs`, with live examples and a "Try it" console
|
|
72
|
+
- validates requests against the spec, supports pagination, filtering, sorting and search,
|
|
73
|
+
and can **simulate latency and errors**
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install api-mock-ai
|
|
77
|
+
api-mock-ai serve openapi.yaml
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```http
|
|
81
|
+
GET /api/users/96da1dac-72ff-4d2a-b86e-cbe06b65a6a4
|
|
82
|
+
|
|
83
|
+
{
|
|
84
|
+
"id": "96da1dac-72ff-4d2a-b86e-cbe06b65a6a4",
|
|
85
|
+
"firstName": "Yaman",
|
|
86
|
+
"lastName": "Ayyubi",
|
|
87
|
+
"email": "yaman.ayyubi18@gmail.com",
|
|
88
|
+
"phone": "+92 308 5918715",
|
|
89
|
+
"role": "customer",
|
|
90
|
+
"address": { "street": "386 Qamar Radial", "city": "Lahore", "postalCode": "92007", "country": "Pakistan" },
|
|
91
|
+
"createdAt": "2026-05-01T16:28:22Z",
|
|
92
|
+
"isVerified": true
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Contents
|
|
99
|
+
|
|
100
|
+
- [Installation](#installation)
|
|
101
|
+
- [Quick start](#quick-start)
|
|
102
|
+
- [Features](#features)
|
|
103
|
+
- [Countries, regions and banking data](#countries-regions-and-banking-data)
|
|
104
|
+
- [Interactive documentation](#interactive-documentation)
|
|
105
|
+
- [CLI reference](#cli-reference)
|
|
106
|
+
- [AI-generated data](#ai-generated-data)
|
|
107
|
+
- [Use it from Python and pytest](#use-it-from-python-and-pytest)
|
|
108
|
+
- [Docker](#docker)
|
|
109
|
+
- [How it works](#how-it-works)
|
|
110
|
+
- [Comparison](#comparison)
|
|
111
|
+
- [Limitations and roadmap](#limitations-and-roadmap)
|
|
112
|
+
- [Development](#development)
|
|
113
|
+
|
|
114
|
+
Full documentation lives in [`docs/`](https://github.com/kashy96/api-mock-ai/blob/main/docs/index.md) and can be served with `mkdocs serve`.
|
|
115
|
+
|
|
116
|
+
## Installation
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
pip install api-mock-ai # Faker-based data, no API key needed
|
|
120
|
+
pip install "api-mock-ai[ai]" # adds LLM support through LiteLLM
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Or install the latest version straight from GitHub:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
pip install "git+https://github.com/kashy96/api-mock-ai.git"
|
|
127
|
+
pip install "api-mock-ai[ai] @ git+https://github.com/kashy96/api-mock-ai.git"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
To run it without installing it into a project, use [pipx](https://pipx.pypa.io) or
|
|
131
|
+
[uv](https://docs.astral.sh/uv/):
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
pipx run api-mock-ai serve openapi.yaml
|
|
135
|
+
uvx api-mock-ai serve openapi.yaml
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Requires Python 3.10 or newer.
|
|
139
|
+
|
|
140
|
+
## Quick start
|
|
141
|
+
|
|
142
|
+
Try it with the bundled example, a small e-commerce API:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
api-mock-ai serve examples/ecommerce.yaml --locale pk
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```text
|
|
149
|
+
+------------------------------- api-mock-ai -------------------------------+
|
|
150
|
+
| ShopSphere Commerce API 2.3.0 | mocked by api-mock-ai 0.1.0 |
|
|
151
|
+
| |
|
|
152
|
+
| API http://localhost:8000/api |
|
|
153
|
+
| Docs http://localhost:8000/docs |
|
|
154
|
+
| Admin http://localhost:8000/__mock__/resources |
|
|
155
|
+
| |
|
|
156
|
+
| Data Faker | 10 per resource | seed 42 |
|
|
157
|
+
| Locale pk: Pakistan (en_PK, currency PKR) |
|
|
158
|
+
| |
|
|
159
|
+
| Resources: /api/users, /api/products, /api/categories, |
|
|
160
|
+
| /api/users/{userId}/orders |
|
|
161
|
+
+---------------------------------------------------------------------------+
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Then:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
curl "http://localhost:8000/api/users?page=1&per_page=5"
|
|
168
|
+
curl "http://localhost:8000/api/products?sort=-price&limit=3"
|
|
169
|
+
curl -X POST http://localhost:8000/api/users \
|
|
170
|
+
-H "Content-Type: application/json" \
|
|
171
|
+
-d '{"firstName": "Ali", "lastName": "Raza", "email": "ali@example.com"}'
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Open **http://localhost:8000/docs** for interactive documentation of *your* API, where
|
|
175
|
+
"Try it" sends real requests to the mock.
|
|
176
|
+
|
|
177
|
+
To check how a spec will be mocked without starting a server:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
api-mock-ai validate examples/ecommerce.yaml
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Features
|
|
184
|
+
|
|
185
|
+
### Realistic data from field names, formats and context
|
|
186
|
+
|
|
187
|
+
| Field | Example value |
|
|
188
|
+
|---|---|
|
|
189
|
+
| `email` (or `format: email`) | `ayesha.khan42@gmail.com`, matching the name in the same object |
|
|
190
|
+
| `firstName`, `lastName`, `fullName`, `username` | one consistent person per object |
|
|
191
|
+
| `name` on a *user* / *product* / *pet* / *category* / *company* | `Ayesha Khan` / `Wireless Headphones` / `Biscuit` / `Electronics` / `Acme Ltd` |
|
|
192
|
+
| `price`, `amount`, `total` | `315.18` |
|
|
193
|
+
| `createdAt`, `updated_at`, `format: date-time` | `2026-05-01T16:28:22Z` |
|
|
194
|
+
| `avatarUrl`, `imageUrl` | `https://picsum.photos/seed/1680/400/300` (a real image) |
|
|
195
|
+
| `phone`, `city`, `country`, `postalCode` | country-aware (`--locale pk`, `in`, `eu`, `de_DE`, ...) |
|
|
196
|
+
| `iban`, `bic`, `bankName`, `accountNumber` | real-format bank details for the country |
|
|
197
|
+
| `status` | `active` / `pending`, or `shipped` / `delivered` on orders |
|
|
198
|
+
|
|
199
|
+
Schema constraints always win: `enum`, `const`, `minimum`/`maximum`, `minLength`/`maxLength`,
|
|
200
|
+
`minItems`/`maxItems`, `allOf`/`oneOf`/`anyOf` and `nullable` are respected, and every
|
|
201
|
+
generated record is valid against its schema (this is tested).
|
|
202
|
+
|
|
203
|
+
### Server-side fields
|
|
204
|
+
|
|
205
|
+
When a request can't contain a field the stored record has (for example
|
|
206
|
+
`POST /transfers` takes only an amount and a payee), the mock fills it in like a real
|
|
207
|
+
server would: ids, timestamps, `readOnly` fields, a reference number, a status. Order
|
|
208
|
+
totals are computed from their line items.
|
|
209
|
+
|
|
210
|
+
### Stateful CRUD
|
|
211
|
+
|
|
212
|
+
Collections are detected automatically from your paths:
|
|
213
|
+
|
|
214
|
+
| Request | Behaviour |
|
|
215
|
+
|---|---|
|
|
216
|
+
| `GET /pets` | list (with filtering, search, sorting, pagination) |
|
|
217
|
+
| `POST /pets` | validate the body, assign an id, add timestamps, store it, `201` |
|
|
218
|
+
| `GET /pets/{petId}` | the stored record, or `404` |
|
|
219
|
+
| `PUT /pets/{petId}` | replace (keeps `id` and `createdAt`) |
|
|
220
|
+
| `PATCH /pets/{petId}` | merge |
|
|
221
|
+
| `DELETE /pets/{petId}` | remove (and its nested children), `204` |
|
|
222
|
+
|
|
223
|
+
Nested collections get several records per parent: every account has its own statement,
|
|
224
|
+
every user has a few orders.
|
|
225
|
+
|
|
226
|
+
Changes live in memory until you restart or call `POST /__mock__/reset`.
|
|
227
|
+
|
|
228
|
+
### Relations
|
|
229
|
+
|
|
230
|
+
- **Nested resources:** `/users/{userId}/orders` only lists that user's orders, returns `404`
|
|
231
|
+
for unknown users, and other users' orders are not reachable through it.
|
|
232
|
+
- **Foreign keys:** fields like `categoryId` or `items[].productId` point to existing records.
|
|
233
|
+
- **Embedded objects:** `product.category` is a copy of a real category.
|
|
234
|
+
- **Custom routes reuse real data:** `GET /users/me` returns one of the generated users.
|
|
235
|
+
|
|
236
|
+
### Lists: pagination, filtering, sorting, search
|
|
237
|
+
|
|
238
|
+
| Query | Effect |
|
|
239
|
+
|---|---|
|
|
240
|
+
| `?status=active` | exact match on any field |
|
|
241
|
+
| `?q=khan` (or `search`, `query`) | case-insensitive search in all text fields |
|
|
242
|
+
| `?sort=price`, `?sort=-price`, `?sort_by=price&order=desc` | sorting |
|
|
243
|
+
| `?limit=10&offset=20`, `?page=3&per_page=10` | pagination (`size`, `pageSize`, `skip`, `take` also work) |
|
|
244
|
+
|
|
245
|
+
If your spec wraps lists in an envelope, it is filled in correctly:
|
|
246
|
+
|
|
247
|
+
```json
|
|
248
|
+
{ "data": [ ... ], "meta": { "total": 57, "page": 3, "per_page": 10, "total_pages": 6 } }
|
|
249
|
+
{ "items": [ ... ], "total": 57, "limit": 10, "offset": 20, "hasMore": true }
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Request validation
|
|
253
|
+
|
|
254
|
+
Bodies are checked against the spec; mistakes get a clear `422`:
|
|
255
|
+
|
|
256
|
+
```json
|
|
257
|
+
{
|
|
258
|
+
"error": "Request body does not match the API spec",
|
|
259
|
+
"status": 422,
|
|
260
|
+
"details": ["species: 'dragon' is not one of ['dog', 'cat', 'rabbit', 'parrot', 'hamster']"]
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
`readOnly` fields are not required in requests, `PATCH` bodies may be partial, and
|
|
265
|
+
`--no-validate` turns validation off.
|
|
266
|
+
|
|
267
|
+
### Chaos testing
|
|
268
|
+
|
|
269
|
+
Test loading spinners and error states in your frontend:
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
api-mock-ai serve openapi.yaml --latency 200-1500 --error-rate 0.1
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Or per request, with headers:
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
curl -H "X-Mock-Delay: 3000" http://localhost:8000/api/users # wait 3 s
|
|
279
|
+
curl -H "X-Mock-Status: 503" http://localhost:8000/api/users # force an error
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Admin endpoints
|
|
283
|
+
|
|
284
|
+
| Endpoint | Purpose |
|
|
285
|
+
|---|---|
|
|
286
|
+
| `GET /docs` | interactive documentation |
|
|
287
|
+
| `GET /openapi.json` | the original spec, pointed at the mock |
|
|
288
|
+
| `GET /__mock__/health` | health check |
|
|
289
|
+
| `GET /__mock__/resources` | detected resources and record counts |
|
|
290
|
+
| `GET /__mock__/data` | every record currently stored |
|
|
291
|
+
| `POST /__mock__/reset` | throw away changes and restore the generated data |
|
|
292
|
+
|
|
293
|
+
## Countries, regions and banking data
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
api-mock-ai serve examples/banking.yaml --locale pk # Pakistan: PKR, +92, HBL/Meezan, PK IBANs
|
|
297
|
+
api-mock-ai serve examples/banking.yaml --locale in # India: INR, +91, SBI/HDFC, IFSC, PAN, UPI
|
|
298
|
+
api-mock-ai serve examples/banking.yaml --locale eu # euro area: each record from another country
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
| `--locale` | Data |
|
|
302
|
+
|---|---|
|
|
303
|
+
| `pk`, `in`, `us`, `uk`, `de`, `fr`, `es`, `it`, `nl` | one country, with its phone format, cities, currency, price level and banks |
|
|
304
|
+
| `eu`, `south_asia` | a region: each record comes from one of its countries |
|
|
305
|
+
| `de_DE`, `ja_JP`, ... | any [Faker locale](https://faker.readthedocs.io/en/master/locales.html) |
|
|
306
|
+
|
|
307
|
+
A sample account generated with `--locale pk`:
|
|
308
|
+
|
|
309
|
+
```json
|
|
310
|
+
{
|
|
311
|
+
"accountTitle": "Wafa Wadood",
|
|
312
|
+
"bankName": "Allied Bank",
|
|
313
|
+
"iban": "PK46ABPA6426212997220033",
|
|
314
|
+
"bic": "ABPAPKAN",
|
|
315
|
+
"accountNumber": "5807302157368193",
|
|
316
|
+
"currency": "PKR",
|
|
317
|
+
"balance": 390420.1
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
The IBAN has valid check digits, and the bank name, BIC and IBAN bank code all describe the
|
|
322
|
+
same bank. Transactions get a type, a matching narration ("Salary credit", "ATM cash
|
|
323
|
+
withdrawal"), a reference and a channel. See [docs/data-packs.md](https://github.com/kashy96/api-mock-ai/blob/main/docs/data-packs.md).
|
|
324
|
+
|
|
325
|
+
## Interactive documentation
|
|
326
|
+
|
|
327
|
+
Every mock serves API documentation at **`/docs`**: endpoints grouped by tag, schemas,
|
|
328
|
+
live examples from the mock, and a "Try it" console with pre-filled ids that exist, error
|
|
329
|
+
simulation and cURL export. It works offline, in light and dark mode, and on phones.
|
|
330
|
+
|
|
331
|
+

|
|
332
|
+
|
|
333
|
+
See [docs/documentation-page.md](https://github.com/kashy96/api-mock-ai/blob/main/docs/documentation-page.md).
|
|
334
|
+
|
|
335
|
+
## CLI reference
|
|
336
|
+
|
|
337
|
+
```text
|
|
338
|
+
api-mock-ai serve SPEC [options] start a mock server
|
|
339
|
+
api-mock-ai generate SPEC [options] print or save mock data as JSON
|
|
340
|
+
api-mock-ai validate SPEC check a spec and show how routes are mocked
|
|
341
|
+
api-mock-ai --version
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
`SPEC` is a path or an `http(s)` URL to an OpenAPI 3.0 or 3.1 document (YAML or JSON).
|
|
345
|
+
|
|
346
|
+
| Option | Default | Description |
|
|
347
|
+
|---|---|---|
|
|
348
|
+
| `--host` | `127.0.0.1` | interface to bind (use `0.0.0.0` in Docker) |
|
|
349
|
+
| `--port`, `-p` | `8000` | port |
|
|
350
|
+
| `--count`, `-n` | `10` | records per resource |
|
|
351
|
+
| `--seed` | `42` | same seed, same data |
|
|
352
|
+
| `--locale`, `-l` | `en_US` | country (`pk`, `in`, `us`, `uk`, `de`...), region (`eu`) or Faker locale |
|
|
353
|
+
| `--ai` | off | generate data with an LLM |
|
|
354
|
+
| `--model`, `-m` | `$API_MOCK_AI_MODEL` | LiteLLM model for `--ai` |
|
|
355
|
+
| `--latency` | `0` | delay in ms: `300` or `100-500` |
|
|
356
|
+
| `--error-rate` | `0` | share of requests (0-1) that fail with 500/502/503 |
|
|
357
|
+
| `--cors / --no-cors` | on | allow requests from other origins |
|
|
358
|
+
| `--validate / --no-validate` | on | validate request bodies |
|
|
359
|
+
| `--base-path` | from `servers` | route prefix, e.g. `/v1`; `/` for none |
|
|
360
|
+
| `--cache-dir` | `.api-mock-ai` | AI data cache |
|
|
361
|
+
| `--refresh` | off | regenerate AI data instead of using the cache |
|
|
362
|
+
| `--output`, `-o` | stdout | (`generate`) file to write |
|
|
363
|
+
|
|
364
|
+
## AI-generated data
|
|
365
|
+
|
|
366
|
+
Faker is fast and free. An LLM understands *meaning*: product descriptions that match
|
|
367
|
+
product names, reviews that match ratings, bios that match job titles.
|
|
368
|
+
|
|
369
|
+
```bash
|
|
370
|
+
pip install "api-mock-ai[ai]"
|
|
371
|
+
|
|
372
|
+
# Local and free, with Ollama
|
|
373
|
+
api-mock-ai serve openapi.yaml --ai --model ollama/llama3.1
|
|
374
|
+
|
|
375
|
+
# Hosted models (LiteLLM reads the usual API key variables)
|
|
376
|
+
export ANTHROPIC_API_KEY=...
|
|
377
|
+
api-mock-ai serve openapi.yaml --ai --model anthropic/claude-haiku-5-5
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Any [LiteLLM model string](https://docs.litellm.ai/docs/providers) works.
|
|
381
|
+
|
|
382
|
+
How it stays reliable:
|
|
383
|
+
|
|
384
|
+
1. The LLM is called **once per resource at startup**, in batches, never per request.
|
|
385
|
+
2. Every value it returns is **validated against the schema**; missing or invalid values are
|
|
386
|
+
replaced with Faker values. A bad model answer can't break your mock.
|
|
387
|
+
3. If the model is unreachable, generation **falls back to Faker** with a warning.
|
|
388
|
+
4. Results are **cached** in `.api-mock-ai/`, keyed by the spec, model, count, seed and locale.
|
|
389
|
+
Restarts are instant and free; use `--refresh` to regenerate.
|
|
390
|
+
|
|
391
|
+
## Use it from Python and pytest
|
|
392
|
+
|
|
393
|
+
```python
|
|
394
|
+
from fastapi.testclient import TestClient
|
|
395
|
+
from api_mock_ai import MockConfig, create_app
|
|
396
|
+
|
|
397
|
+
app = create_app("openapi.yaml", MockConfig(count=20, locale="uk", seed=7))
|
|
398
|
+
client = TestClient(app)
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def test_checkout_flow():
|
|
402
|
+
product = client.get("/api/products").json()["items"][0]
|
|
403
|
+
...
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
`create_app` returns a normal FastAPI app, so you can also serve it with any ASGI server.
|
|
407
|
+
|
|
408
|
+
Pre-generated data (`api-mock-ai generate spec.yaml -o data.json`) is useful as test
|
|
409
|
+
fixtures or for seeding a development database.
|
|
410
|
+
|
|
411
|
+
## Docker
|
|
412
|
+
|
|
413
|
+
```bash
|
|
414
|
+
docker build -t api-mock-ai .
|
|
415
|
+
docker run --rm -p 8000:8000 -v "$PWD/openapi.yaml:/spec/openapi.yaml" api-mock-ai
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
## How it works
|
|
419
|
+
|
|
420
|
+
```text
|
|
421
|
+
openapi.yaml
|
|
422
|
+
|
|
|
423
|
+
v
|
|
424
|
+
+-------------+ +---------------------+ +-----------------+
|
|
425
|
+
| Spec loader |-->| Resource detection |-->| Seed generator |
|
|
426
|
+
| YAML / JSON | | collections, items, | | Faker or LLM, |
|
|
427
|
+
| $ref resolve| | parents, id fields | | linked + cached |
|
|
428
|
+
+-------------+ +---------------------+ +--------+--------+
|
|
429
|
+
v
|
|
430
|
+
+-----------------------------------------------+
|
|
431
|
+
| FastAPI server |
|
|
432
|
+
| routing -> validation -> store (CRUD) -> shape|
|
|
433
|
+
| + CORS, chaos middleware, admin API, docs |
|
|
434
|
+
+-----------------------------------------------+
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
See [docs/how-it-works.md](https://github.com/kashy96/api-mock-ai/blob/main/docs/how-it-works.md) for the design decisions.
|
|
438
|
+
|
|
439
|
+
## Comparison
|
|
440
|
+
|
|
441
|
+
| | Static examples | Random schema values | **api-mock-ai** |
|
|
442
|
+
|---|---|---|---|
|
|
443
|
+
| Looks like real data | only where examples exist | no (`"string"`, `"abc123"`) | yes |
|
|
444
|
+
| Remembers `POST`/`DELETE` | no | no | yes |
|
|
445
|
+
| Consistent relations | no | no | yes |
|
|
446
|
+
| Optional LLM generation | no | no | yes, cached |
|
|
447
|
+
|
|
448
|
+
## Limitations and roadmap
|
|
449
|
+
|
|
450
|
+
Current limitations (contributions welcome):
|
|
451
|
+
|
|
452
|
+
- Swagger 2.0 must be converted to OpenAPI 3 first.
|
|
453
|
+
- `$ref`s to *other files* are not resolved yet (local `#/...` refs are).
|
|
454
|
+
- `pattern` constraints on strings are not used for generation.
|
|
455
|
+
- Data lives in memory; it resets on restart.
|
|
456
|
+
- Non-JSON bodies (XML, form data, file uploads) are accepted but not validated.
|
|
457
|
+
|
|
458
|
+
Planned: external `$ref`s, persisting data to a file, response examples as hints, webhooks,
|
|
459
|
+
a GitHub Action, and a small benchmark of data realism against other mock tools.
|
|
460
|
+
|
|
461
|
+
## Development
|
|
462
|
+
|
|
463
|
+
```bash
|
|
464
|
+
git clone https://github.com/kashy96/api-mock-ai
|
|
465
|
+
cd api-mock-ai
|
|
466
|
+
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
467
|
+
pip install -e ".[dev,ai]"
|
|
468
|
+
pytest --cov
|
|
469
|
+
ruff check . && ruff format --check .
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
See [CONTRIBUTING.md](https://github.com/kashy96/api-mock-ai/blob/main/CONTRIBUTING.md) and [CHANGELOG.md](https://github.com/kashy96/api-mock-ai/blob/main/CHANGELOG.md).
|
|
473
|
+
|
|
474
|
+
## License
|
|
475
|
+
|
|
476
|
+
[MIT](https://github.com/kashy96/api-mock-ai/blob/main/LICENSE)
|