|
| 1 | +--- |
| 2 | +title: Mock REST and Kafka APIs Using OpenAPI & AsyncAPI |
| 3 | +description: Mock REST and Kafka APIs with Mokapi using OpenAPI or AsyncAPI. Simulate real world scenario using JavaScript — all in a single tool. |
| 4 | +--- |
| 5 | + |
| 6 | +# Mock REST and Kafka APIs with OpenAPI & AsyncAPI Using Mokapi |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +Mocking APIs speeds up development—especially when services are incomplete or unavailable. Whether you’re building a |
| 11 | +frontend, testing integrations, or developing in parallel, reliable mocks keep you productive. |
| 12 | + |
| 13 | +Mokapi is an open-source tool designed to mock APIs like REST and Kafka using OpenAPI and AsyncAPI specifications. |
| 14 | +With Mokapi, you can simulate API responses, customize behavior with scripts, and—most importantly—debug everything |
| 15 | +visually with a built-in dashboard. |
| 16 | +The dashboard shows incoming HTTP requests, Kafka events, your JavaScript event handlers, and scheduled jobs—making it |
| 17 | +a powerful tool for debugging, understanding behavior, and refining your mocks. |
| 18 | + |
| 19 | +In this article, you'll learn: |
| 20 | +- What Mokapi is and why it's useful |
| 21 | +- How to mock a REST API using OpenAPI |
| 22 | +- How to mock a Kafka topic using AsyncAPI |
| 23 | +- How to extend mocks with custom behavior |
| 24 | +- How to integrate Mokapi into your dev workflow |
| 25 | + |
| 26 | +## What is Mokapi? |
| 27 | + |
| 28 | +Mokapi is a lightweight, developer-friendly tool for mocking REST and Kafka services. It supports OpenAPI and AsyncAPI |
| 29 | +specs out of the box and allows you to: |
| 30 | +- Serve mock HTTP APIs |
| 31 | +- Simulate Kafka topics and producers |
| 32 | +- Write custom logic using JavaScript |
| 33 | +- Visualize interactions in a web dashboard in real time\ |
| 34 | + perfect for debugging and understanding mock behavior. |
| 35 | + |
| 36 | +Mokapi is written in Go and available as a single binary, making it easy to run locally or in CI environments. |
| 37 | + |
| 38 | +## Yet Another Mocking Tool? |
| 39 | + |
| 40 | +Yes — but with a twist. |
| 41 | + |
| 42 | +The focus of Mokapi is to ensure API specification compliance. For example, every incoming HTTP request is validated against the OpenAPI specification, and the response you define is also validated before it’s returned. This helps prevent contract mismatches early in development. |
| 43 | + |
| 44 | +What makes Mokapi special compared to other mocking tools: |
| 45 | +- Strict validation: Requests and responses must conform to your OpenAPI/AsyncAPI spec. |
| 46 | +- Realistic data generation: You can use examples from your spec or generate meaningful mock data. |
| 47 | +- Dynamic scripting: Inject behavior with JavaScript to simulate conditional logic, delays, or even stateful responses. |
| 48 | +- Multiprotocol support: Supports both REST and Kafka-style messaging in one tool. |
| 49 | +- Live dashboard: See all requests, mock responses, and Kafka events in real time. |
| 50 | +- Developer-friendly: Single binary, easy CLI, scriptable and everything as code. |
| 51 | + |
| 52 | +## Mocking a REST API with OpenAPI |
| 53 | + |
| 54 | +### 1. Create an OpenAPI file |
| 55 | + |
| 56 | +```yaml |
| 57 | +openapi: 3.1.0 |
| 58 | +info: |
| 59 | + title: Pet API |
| 60 | + version: 1.0.0 |
| 61 | +servers: |
| 62 | + - url: http://localhost |
| 63 | +paths: |
| 64 | + /pets: |
| 65 | + get: |
| 66 | + summary: List all pets |
| 67 | + parameters: |
| 68 | + - in: query |
| 69 | + name: category |
| 70 | + schema: |
| 71 | + type: string |
| 72 | + responses: |
| 73 | + '200': |
| 74 | + description: A list of pets |
| 75 | + content: |
| 76 | + application/json: |
| 77 | + schema: |
| 78 | + type: array |
| 79 | + items: |
| 80 | + type: object |
| 81 | + properties: |
| 82 | + id: |
| 83 | + type: string |
| 84 | + name: |
| 85 | + type: string |
| 86 | + category: |
| 87 | + type: string |
| 88 | +``` |
| 89 | +
|
| 90 | +### 2. Run Mokapi with your OpenAPI spec |
| 91 | +
|
| 92 | +```shell |
| 93 | +mokapi pet-api.yaml |
| 94 | +``` |
| 95 | + |
| 96 | +### 3. Call the API |
| 97 | + |
| 98 | +```shell |
| 99 | +curl http://localhost/pets |
| 100 | +curl "http://localhost/pets?category=dog" |
| 101 | +``` |
| 102 | + |
| 103 | +Mokapi will generate random data for those requests. If you query with ?category=dog, Mokapi will return only dogs |
| 104 | +thanks to smart mock data generation. |
| 105 | + |
| 106 | +### Adding Custom Logic |
| 107 | + |
| 108 | +You can add a JavaScript file to dynamically control responses: |
| 109 | + |
| 110 | +```javascript |
| 111 | +import { on } from 'mokapi'; |
| 112 | +import { fake } from 'mokapi/faker'; |
| 113 | + |
| 114 | +export default () => { |
| 115 | + on('http', (request, response) => { |
| 116 | + if (request.query.category === 'cat') { |
| 117 | + response.data = [ |
| 118 | + { |
| 119 | + id: fake({ type: 'string', format: 'uuid' }), |
| 120 | + name: 'Molly', |
| 121 | + category: 'cat' |
| 122 | + } |
| 123 | + ]; |
| 124 | + return true; |
| 125 | + } |
| 126 | + return false; |
| 127 | + }); |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +Run Mokapi with the script: |
| 132 | +```shell |
| 133 | +mokapi pet-api.yaml pets.js |
| 134 | +``` |
| 135 | + |
| 136 | +## Mocking Kafka with AsyncAPI |
| 137 | + |
| 138 | +### 1. Create an AsyncAPI file |
| 139 | + |
| 140 | +```yaml |
| 141 | +asyncapi: 3.0.0 |
| 142 | +info: |
| 143 | + title: Petstore Stream API |
| 144 | + version: 1.0.0 |
| 145 | +servers: |
| 146 | + kafkaServer: |
| 147 | + host: localhost:8092 |
| 148 | + protocol: kafka |
| 149 | +defaultContentType: application/json |
| 150 | +operations: |
| 151 | + receivePetArrived: |
| 152 | + action: receive |
| 153 | + channel: |
| 154 | + $ref: '#/channels/store.pet.arrived' |
| 155 | + sendPetArrived: |
| 156 | + action: send |
| 157 | + channel: |
| 158 | + $ref: '#/channels/store.pet.arrived' |
| 159 | +channels: |
| 160 | + store.pet.arrived: |
| 161 | + description: Details about a newly arrived pet. |
| 162 | + messages: |
| 163 | + userSignedUp: |
| 164 | + $ref: '#/components/messages/petArrived' |
| 165 | +components: |
| 166 | + messages: |
| 167 | + petArrived: |
| 168 | + payload: |
| 169 | + $ref: '#/components/schemas/Pet' |
| 170 | + schemas: |
| 171 | + Pet: |
| 172 | + id: |
| 173 | + type: string |
| 174 | + name: |
| 175 | + type: string |
| 176 | + category: |
| 177 | + type: string |
| 178 | +``` |
| 179 | +
|
| 180 | +### 2. Create a Kafka producer |
| 181 | +
|
| 182 | +```javascript |
| 183 | +import { every } from 'mokapi'; |
| 184 | +import { produce } from 'mokapi/kafka'; |
| 185 | + |
| 186 | +export default () => { |
| 187 | + every('10s', () => { |
| 188 | + produce({ topic: 'store.pet.arrived' }); |
| 189 | + }); |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +### 3. Run Mokapi with your AsyncAPI spec |
| 194 | + |
| 195 | +```shell |
| 196 | +mokapi pet-stream-api.yaml producer.js |
| 197 | +``` |
| 198 | + |
| 199 | +Mokapi will simulate Kafka messages and allow you to inspect them via the dashboard. |
| 200 | + |
| 201 | +## Integrating with Your Dev Workflow |
| 202 | + |
| 203 | +- Use Mokapi in CI pipelines to test frontend against mocked backends |
| 204 | +- Simulate Kafka events for microservice testing |
| 205 | +- Share mock configurations with your team/organization |
| 206 | + |
| 207 | +You can use Mokapi in a GitHub action run as docker container to streamline your automation. |
| 208 | + |
| 209 | +```yaml |
| 210 | +- name: Start Mokapi |
| 211 | + run: | |
| 212 | + docker run -d --rm --name mokapi -p 80:80 -p 8080:8080 -v ${{ github.workspace }}/mocks:/mocks mokapi/mokapi:latest /mocks |
| 213 | +``` |
| 214 | +
|
| 215 | +## Conclusion |
| 216 | +
|
| 217 | +Mokapi helps you mock REST and Kafka services quickly using standard API specs. Whether you’re building, testing, |
| 218 | +or demoing your application, Mokapi provides a simple yet powerful way to simulate realistic API behavior. |
| 219 | +
|
| 220 | +Give it a try: [https://mokapi.io](https://mokapi.io) |
0 commit comments