SimplyBook (Search or Create)#

This example will walk you through creating a write action and a search action, which will then be “upgraded” to create a “Search or Create” action. To make the example real, we'll be implementing the SimplyBook API.

Quick Preparation Checklist#

We’ll assume having started with the SimplyBook app from the Session Auth example. Make sure you get all the links and code from there (or understand it to grasp how we’re extending it).

Add Create Client Action#

First, we need to create an action to Create a Client.

Add Your First Action button

Step 1#

We fill the first step like this:

Step 1 of Create Client action

Step 2#

To not overcomplicate this example, we’ll only add 3 fields: - Name (required) - Email - Phone

For that we’ll need to use the “Add New Action Field” button.

Add New Action Field button

And here’s how each field would look:

Name Action Field

Email Action Field

Phone Action Field

All of them should have checked the “Send in POST by default” checkbox

Send in POST by default checkbox

This is how they look in the end for the action’s step 2:

Step 2 of Create Client action

Step 3#

For Step 3 we only need to set the https://user-api.simplybook.me/admin as the Action Endpoint URL

Step 3 of Create Client action

Step 4#

In the last step, the API only returns the client ID, so that’s what we’ll add:

{
    "id": 1
}

Step 4 of Create Client action

This API requires us to do some changes in the Scripting (your API might not), so we’ll add the following methods:

var Zap = {
    // other methods...

    // Modify the request details before creating a client
    create_client_pre_write: function(bundle) {
        bundle = this.add_custom_headers(bundle);

        // Build Request Body
        var request_data = {
            jsonrpc: '2.0',
            method: 'addClient',
            params: [bundle.action_fields],
            id: 1
        };

        bundle.request.data = z.JSON.stringify(request_data);

        return bundle.request;
    },

    // Check if there's an invalid session
    create_client_post_write: function(bundle) {
        var parsed_response = z.JSON.parse(bundle.response.content);

        // Handle errors (ideally we'd look in the response status_code)
        if (parsed_response && parsed_response.error) {
            throw new InvalidSessionException();
        }

        var result = {
            id: parsed_response.result.message
        };

        return result;
    }
};

We’re now ready to test the action! Zap away!

Add Search Client Action#

Now, we need to create a Search for Clients.

Add Your First Search button

Step 1#

We fill the first step like this:

Step 1 of Find Client search

Step 2#

The API allows us to send a string which searches in name, email, and phone, so we only need 1 field:

Query Search Field

It should look like this:

Step 2 of Find Client search

Step 3#

We’ll leave this step empty for now, and will come back to it later. Just click “Save & Next”.

Step 4#

Here’s how Step 4 should look. We’re using the same https://user-api.simplybook.me/admin URL because this API changes “what’s called” in the request body.

Step 4 of Find Client search

Step 5#

For the final step, we add the sample JSON for a single object:

{
    "name": "Test Client",
    "phone": "+1111-111-1111",
    "email": "test@example.com"
}

Step 5 of Find Client search

This API requires us to do some changes in the Scripting (your API might not), so we’ll add the following methods:

var Zap = {
    // other methods...

    // Modify the request details before finding a client
    find_client_pre_search: function(bundle) {
        bundle = this.add_custom_headers(bundle);

        // Build Request Body
        var request_data = {
            jsonrpc: '2.0',
            method: 'getClientList',
            params: [bundle.search_fields.query],
            id: 1
        };

        bundle.request.data = z.JSON.stringify(request_data);

        return bundle.request;
    },

    // Check if there's an invalid session
    find_client_post_search: function(bundle) {
        var parsed_response = z.JSON.parse(bundle.response.content);

        // Handle errors (ideally we'd look in the response status_code)
        if (parsed_response && parsed_response.error) {
            throw new InvalidSessionException();
        }

        return parsed_response.result;
    },

    // Modify the request details before fetching a client
    find_client_pre_read_resource: function(bundle) {
        bundle = this.add_custom_headers(bundle);

        // NOTE: This API doesn't really have a "GET" endpoint,
        // so we search for email and hope for the best.

        // Build Request Body
        var request_data = {
            jsonrpc: '2.0',
            method: 'getClientList',
            params: [bundle.read_fields.email],
            id: 1
        };

        bundle.request.data = z.JSON.stringify(request_data);

        return bundle.request;
    },

    // Check if there's an invalid session
    find_client_post_read_resource: function(bundle) {
        var parsed_response = z.JSON.parse(bundle.response.content);

        // Handle errors (ideally we'd look in the response status_code)
        if (parsed_response && parsed_response.error) {
            throw new InvalidSessionException();
        }

        var result = [];

        // Only return the object
        if (parsed_response.result && parsed_response.result.length > 0) {
            result = parsed_response.result[0];
        }

        return result;
    }
};

Now we have a working Create Client action and Find Client search!

Upgrading to Search or Create#

Our app allows us to create clients and search for them, but you can upgrade the search to automatically create a client if it’s not found. This is incredibly powerful, and easy to implement.

We just need to edit our search and go to step “3. Search or Action”.

In our case, we just need to link these to the create action we already have:

Step 3 of Find Client search

Now we can see it in action!

Editor with Find or Create Client

Congratulations! You have a working SimplyBook application that:

  • Has a visible write action.
  • Has a visible search or create action.

Be sure to check out our other examples for more details on doing other interesting things with Zapier's developer platform!

↑ Was this documentation useful? Yes No
Get Help