Apiary and API call mocking
Last modified on Wed 18 Oct 2023

What is Apiary?

Apiary is a tool that lets you build a functional mocked API really fast, in a single day or even in a few hours.

How does it work?

You can look at Apiary like an online collection of JSON responses with a bit more power. You get some base URL and all you have to do is define endpoints and their responses. An example of one such response can be seen below:


## Some API Endpoint [/apiEndpoint]

### GET some information [GET]

+ Response 200 (application/json)

        [
            {
                "information": "Favourite programming language?",
                "published_at": "2015-08-05T08:40:51.620Z",
                "choices": [
                    {
                        "choice": "Swift",
                        "votes": 1
                    }
                ]
            }
        ]

### POST some information [POST]

+ Response 201 (application/json)

    + Headers

            Location: /collection/2

    + Body

            {
                "information": "Favourite programming language?",
                "published_at": "2015-08-05T08:40:51.620Z",
                "choices": [
                    {
                        "choice": "Swift",
                        "votes": 2
                    } 
                ]
            }

Apiary usually uses API Blueprint, a powerful high-level API description language for web APIs, more details on that can be found here.

Why Apiary?

Some of the reasons why you might want to add Apiary to your project:

Know your limits

Some of the Apiary limitations:

Setup

Step 1

Step 2

Step 3

Step 4

At this moment setup is done and you should end up with something like this:

Editor

[ 1 ] = Base URL

[ 2 ] = API Endpoint

[ 3 ] = Method that will be used, GET / POST / DELETE ...

[ 4 ] = In case you liked Apiary and want to create a bigger team.

[ 5 ] = Save button, you should press it after every change

[ 6 ] = Add up to 5 people to project for free

[ 7 ] = Navigation between multiple APIs

Now all you have to do is add a bunch of endpoints along with their responses and voila, you have your API mock :)

Connecting your app

There are multiple ways to connect Apiary with your app: you can use mocking for the entire app or only a few screens. In the example below will be shown how to set up a mock for the entire app.

Step 1

enum Base: String { 
    case api = "https://backend.project.com/something/api" 
    case mock = "https://private-4d7af-project18.apiary-mock.com" 
}

Step 2

class Selection {
    static let current = Selection()

    private(set) var base: Constants.AppUrl.Base = .api 
}

Step 3

    Selection.current = .mock

    // Login
#if STORE 
    mockLoginButton.isHidden = true
#endif

And that should be it. Your app is ready for some mocking!

Closing words

There is much more Apiary can offer you. This chapter covers only basic, however, there is also a paid version that provides much more functionality. You can find more info here.