HACKER Q&A
📣 HiddenCanary

Best Way to Mock APIs in 2020?


I'm looking for a good way to Mock APIs for a sideproject, so far I haven't found any solution that has blown me away and I'm wondering If I'm missing anything.

I'm curious what people recommend for the best way to Mock APIs?


  👤 triztian Accepted Answer ✓
I've found mitmproxy (https://mitmproxy.org) in reverse mode really useful. I'm a mobile dev and when the backend work is still pending I usually mock the backend requirements by selectively patching or adding endpoint support, it can be used like so:

    mitmproxy --mode reverse:https://my.privateapi.com -s ./path/to/mockscript.py

This allows me to implement most of the UI and server interactions w/o being blocked by server work.

👤 peheje
Can someone explain to me what this is all about? Shouldn't you hide all your API calls behind an interface, and then mock that interface? Dependency inversion principle, depend upon abstractions and all that.

👤 robjan
Depends how advanced the mocking behaviour requirements are. For most projects, I find json-server sufficient.

Remember that perfect is the enemy of good enough. If you're looking to be blown away by your mock server you may never get round to building your product.


👤 nerdywordy
https://miragejs.com/ is a great complement to frontend first development. Some folks on my team use it and love it. I tend to dev from back to front so I don't run into the need as much. If I do I typically just mock a JSON response in my frontend call function. Depending on the need I'll sometimes set the call to return an error response as well.

👤 jcpst
For a side project? I think that makes a difference here.

What I’ve been doing lately, in my own time outside of work, is setting up a postgres DB, pointing postgraphile to it, then I have the API out of the way.

From there, if I’m consuming that API in a statically-typed language, I’ll look at generating the models I need from the graphql schema with quicktype.

That process is usually quick enough that there’s no reason to mock.

I suppose if I was doing UI first, I would just have the repository layer that’s abstracting my calls to whatever just have a function that returns a dumb static object in the shape I expect.


👤 zoontek
Might not be the best, but the ones I use:

For REST API: https://mockoon.com/

For GraphQL: https://github.com/APIs-guru/graphql-faker


👤 kahrensdd
There are lots of variations depending upon your use case.

For unit testing and CI you may want mock objects that are implemented in the same language as your code. Google search for "mock object ". That's where you'll find Mockito (Java) or Mocha Spy (NodeJS) or Testify (golang). This list never ends.

Specifically for unit testing of a UI, you may want your browser driver to handle this, ex: Cypress has built-in support for mock AJAX endpoints. https://docs.cypress.io/guides/guides/stubs-spies-and-clocks...

If you want an endpoint you can call, Postman has a feature for this, there are several others like this in the comments (JSON Server, mmock, mountebank, etc.). https://learning.postman.com/docs/postman/mock-servers/setti...

If you need to capture traffic, check out goreplay or mitmproxy: https://github.com/buger/goreplay https://docs.mitmproxy.org/stable/

There is a whole class of "VCR" projects for recording traffic, these tend to be language specific (VCR is in Ruby), but there are ports to other languages: https://github.com/vcr/vcr https://github.com/bblimke/webmock

The vendor products tend to be labelled Service Virtualization. I used to work for one of those companies, ITKO, we were acquired by CA Technologies (now Broadcom) in 2011. There are vendor products from Micro Focus, Tricentis, Broadcom, Parasoft, etc.

It's important to think about your use case: local development, unit testing, CI, integration testing, performance testing, recording vs. programming, protocol support, payload support, etc. Many of the tools focus on just a subset of these areas.


👤 forresponse
I've used this on multiple projects: https://github.com/typicode/json-server

👤 tiborsaas
I've used this: https://github.com/nock/nock worked great for tests.


👤 baggsie
I've found Hasura to be great for this, maybe slightly more longer to set up but you get a full CRUD graphql setup for little effort.


👤 m0xte
http://wiremock.org/ works for me

👤 mdaniel
I readily admit I haven't evaluated every one of the comprehensive URLs in this thread, but back when I was doing integration work between my project and both GitHub and Jira, I found the scripting support in SoauUI (https://github.com/smartbear/soapui/tree/release-5.5.0) allowed me to model some dynamic behavior, and use the fallback responses when I didn't care about specifying every single one of them

Despite its name, SoapUI has strong support for REST, and with a little massaging maybe even GraphQL also


👤 xgenecloud
If you have a database - try this https://github.com/xgenecloud/xgenecloud

Automatically generates REST & GraphQL APIs within seconds from your database schema.

Here is a demo on how to create 8000+ REST APIs within 10 seconds - https://youtu.be/NtCwnlLudnk

Not just mock - you may even consider writing the full backend on it.

Im the creator - happy to answer any questions.

edit : added demo link


👤 disposedtrolley
If the API provider supplies an OpenAPI definition, you can give API Sprout a try. It’ll parse the definition and spin up a mock server returning the example data you want. You can also modify the OpenAPI definition manually if you need different data to be returned.

https://github.com/danielgtaylor/apisprout


👤 jacobr
Haven’t tried it yet, but https://www.npmjs.com/package/msw looks very interesting. It uses Service Workers in the browser to intercept requests to the APIs, instead of running a real mock server.

There’s support for mocking in Node as well for SSR and jsdom tests.


👤 pinacarlos90
Use swagger-openAPI3 and generate a spec. You can then use the spec to spin up clients or import to a API management tool and produce mock data, with support for authentication and more. I recommend using swashbuckle (for .netCore) if you already have the code for your API, it allows you to quickly produce spec from code.

👤 coold
I'm using Wiremock

👤 thiht
Shameless plug: I developed Smocker[1] for exactly this use case. It's not the only solution but we have a few features not present anywhere else that matched exactly our use case.

Hope it can help you :)

[1]: https://smocker.dev


👤 mcintyre1994
I’ve really liked working with mmock https://github.com/jmartin82/mmock

It has a nice API for inspecting the requests made to the mock server and a nice web UI for debugging too.


👤 lbroudoux
I would suggest having a look at Microcks (https://microcks.io) as a way to produce mocks from OpenAPI contracts, Postman collection or other assets...

Full disclosure : I am the founder of the project ;-)


👤 tomakehurst
MockLab [1] is a SaaS mocking tool built on top of WireMock.

Worth a look if you use Swagger/OpenAPI as it'll accept this as an import format.

[1]: http://mocklab.io


👤 davegb3
If you like wiremock, try https://intervene.dev - config in TypeScript with automatic https and CORS.

Open for questions + feedback [author]


👤 isuckatcoding
Don’t bother with mock APIs tbh. I assume you’re trying to unit test the front end?

If anytime, write integration tests using something like selenium or whatever the latest hotness is.


👤 sidcool
I use Mountebank extensively and it's a pleasure to use.

👤 htyden
https://intervene.dev/ has been very useful for me.


👤 hckr_news
Nock and JSON server are useful libraries

👤 lovetocode
A lot of Ruby’s test frameworks have this functionality built in. What technology are you using

👤 mister_hn
Api-framework works really good for this

👤 vkku
Mockito exists as an option

👤 nmasse-itix
I was wondering if you had a chance to have a look at Microcks (https://microcks.io/) ?