# Serverless Sales Automation

How to capture and organise your customers with Typeform and Infusionsoft.

- **Author:** Dmitry Zaets
- **Published:** 2018-06-14
- **Updated:** 2018-06-14
- **Tags:** articles, aws, serverless, typeform, sales-automation, process-automation

---

import Figure from '../../src/components/Figure.astro'

As you probably know, serverless computing is on the rise. It is not the
solution to all our problems, but there are many use-cases, and some are a great
fit to Lambdas (serverless functions on Amazon infrastructure). Asynchronous
integrations between services are a perfect example.

We wanted to get more quality leads to grow our business and use
[Typeform](http://typeform.com) for customer segmentation and qualification.
This data, we aspired to process in our CRM software
[Infusionsoft](http://infusionsoft.com) to nurture leads and take them through
the sales funnel.

Typeform delivers the results of forms through webhooks, which we can direct to
our Lambda for processing. The call itself is lightweight and does not require a
lot of resources (parsing data), and with Lambda functions you pay only per
execution time (no need to pay for running containers, but also a lot of free
processing time).

## Setting up Serverless

We decided to use the popular [Serverless Framework](https://serverless.com) to
deploy code to Amazon Web Services (AWS) as a cloud provider. The framework
makes deployments a lot easier and has a lot of features including cross-cloud
deployments, local emulators, and more.

To get started with Serverless we would need to install it globally:

    npm i serverless -g

Then we need to create a file called `serverless.yml` which will define
structure of our cloud functions:

<script src="https://gist.github.com/PatrickHeneise/c566e08766d3858a3b8ed98892883e05.js"></script>

We define the name of the service, plugins that we would need, the cloud
provider and the functions declaration.

### Plugins

As our integration is fairly easy and does not use any other components than the
function itself, we do not need any plugins. `serverless-offline` is only used
for local testing and the emulator.

In order to install the plugin you need to do two things:

1. Install the node.js module (Because of the plugin is used for the development
   we will install it as dev dependency)
1. Add plugin to the plugins section of the serverless.yml file

   npm install serverless-offline --save-dev

### Provider

As we are on AWS, we use “aws” in the provider definition as well as node8.10
(_yay, finally!_) as runtime. `profile` is the credential used to deploy the
function (see
[serverless docs on credentials and profiles](https://serverless.com/framework/docs/providers/aws/guide/credentials/#using-aws-profiles)).

### Functions

We are integrating a webhook from Typeform (see
[Typeform docs on webhooks](https://developer.typeform.com/webhooks/)), so we
call our handler `typeform-webhook`, setting the path accordingly and the
function to be called. Then we specify on which events this function needs to be
called — in our case we would like to call it when a POST request is performed
to path `webhooks/typeform`.

## Deploying the function

Deployment of Serverless functions could not be easier. All that we need are the
credentials to our cloud provider (see
[Creating AWS Access Keys](https://serverless.com/framework/docs/providers/aws/guide/credentials/#creating-aws-access-keys)),
then we simply execute:

```
serverless deploy
```

Once the function is deployed, you can see the URL to call the function in the
serverless output. Copy that URL into the Typeform webhook configuration
(Integrate → Webhooks on your form).

![Typeform Webhook Configuration](/images/articles/1*eWIEpelnw6i1bGABNY8pNg.png)_Typeform
Webhook Configuration_

We can easily test if the function is reachable and if it works as we expect it
to, just by pressing “Test Webhook” button.

If all goes well, Typeform performs a test call and successfully submits test
data. We can open our AWS console to see the result of the call.

## Processing Data

The webhook payload is divided into two major parts: `definition` and `answers`.
`definition` contains the questions and response types; `answers` includes the
obvious: the data a visitor put into the form. Although obvious, Typeform makes
it a little tricky to extract the answers from the array. We took the easiest
path using the indexes and ids of the definitions to retrieve the data we need
for our Infusionsoft API call.

Once all the data is parsed, we can use any http/https client library such as
[r2](https://www.npmjs.com/package/r2) or [axios](http://The webhook payload is
divided into two major parts: definition and answers. Definition contains the
questions and response types; answers includes the obvious: the data a visitor
put into the form. Although obvious, Typeform makes it a little tricky to
extract the answers from the array. For now, we're using the index of the answer
to retrieve the data we need for our Infusionsoft API call.) to call the
Infusionsoft API and create a new contact. We only need to retrieve an API token
and set it as `INFUSION_ACCESS_TOKEN` in `.env` file:

<Figure
  src="/images/articles/1*ixqN8Ba7V83RxAMviqETGw.png"
  title="Infusionsoft Account Central"
  width="3840"
  height="2400"
/>
<Figure
  src="/images/articles/1*OHSQgdYWyVO-TixpfgJoXQ.png"
  title="Infusionsoft OAuth Credentials"
  width="3840"
  height="2400"
/>
<Figure
  src="/images/articles/1*f1-5GBalTunzIrxNIB7lhA.png"
  title="Access Token"
  width="3840"
  height="2400"
/>
<Figure
  src="/images/articles/1*ZQIEWf25ePJrELCEidRwYg.png"
  title="Copy Access Token Example"
  width="530"
  height="378"
/>

## Summary & Conclusion

Serverless technology makes it really easy and cheap to integrate different
software products through their APIs or webhooks. It is possible to act or react
to certain events. These integrations can go far beyond “If This Then That” or
Zapier, as we can use any node.js library (machine learning, natural language
processing, to name a few) and combine those with the simplicity of filling out
a Typeform.

If you have any questions or comments, please
[reach out on Bluesky](https://bsky.app/profile/zentered.co) or
[start a discussion on GitHub](https://github.com/zentered/zentered/discussions/new?category=ideas-feedback).
