
Mock API? - itshabib
I’m curious to hear what everyone uses  or does whenever they are developing and need to interact with an API that hasn’t been created or whos actual contents doesn’t necessarily matter yet. Do you tend to hard code responses that an api can return? Do you mock the API another way?
======
tedmiston
For early stage and hackathon-style API mocking, JSON Server is pretty cool.
You implement a JSON file of objects that gets inferred as endpoints and
served up as an API.

[https://github.com/typicode/json-server](https://github.com/typicode/json-
server)

For a fancier solution, Swagger / OpenAPI spec is useful. It has a feature
called API Auto Mocking which you can serve with SwaggerHub. [The free tier
includes this feature by the way.]

[https://app.swaggerhub.com/help/integrations/api-auto-
mockin...](https://app.swaggerhub.com/help/integrations/api-auto-mocking)

[https://swagger.io/tools/swaggerhub/](https://swagger.io/tools/swaggerhub/)

You can also use Swagger Codegen to generate server stubs and API clients as
code in various languages once you're ready to break out of the mock API.

[https://github.com/swagger-api/swagger-codegen](https://github.com/swagger-
api/swagger-codegen)

That's the route I'd go personally as this tooling is available for free, is
fairly mature, and has a strong community behind it at this point.

There are a few other similar alternatives that I know less about such as
WireMock ([http://wiremock.org/](http://wiremock.org/)) and Stoplight
([https://stoplight.io/](https://stoplight.io/)). Postman also has a mock
server feature if you happen to already use that.

------
dmlittle
If you need a public HTTP endpoint to test different response code but not
payloads feel free to use mock.codes[1].

I built this a couple of years ago when I wanted to experiment with AWS API
Gateway. The entire project is hard-coded definitions in API Gateway with a
domain (I don't log anything). It costs me a trivial amount to host
(~$0.51/mo, 99% of which is the R53 hosted zone + domain cost) so I'll be
keeping it up indefinitely since I use it for some projects of mine.

[1] [https://mock.codes](https://mock.codes)

------
fatnoah
I've done this a couple times in the past for projects where I owned the back-
end and the front-end was outsourced. I documented the API spec, mocked the
API to return hard-coded values, and wrote tests to cover the expected test
cases.

It was a bit of work up front, but it made the eventual integration almost
seamless. In this case, I manually created the endpoints and mocked return
values. If I did it again, I'd probably use something more automated like some
of the other posters have mentioned.

------
sebmak
I personally will usually mock out the api using Node/Express and link
endpoints to hardcoded json files.

------
zha
Flask app with programmatically generated mock data with variations. nginx to
connect the whole thing.

------
wingerlang
When I just need to read some response content I chuck some JSON on
pastebin.com

