python gravity forms api

Gravity Forms is a popular WordPress form plugin. If you are a leads based business you will want to know that your form is working and that leads can contact you. We’ve all had the feeling of looking at the entry log and seeing an unusual gap between the last entry and the current moment. Maybe there was a glitch. How would you know? Feel free to browse the Gravity Form API documentation before we begin. Let’s get started!

Requirements and Assumptions

Starting the Script

First let’s import the yagmail so we can send an email .

pip3 install yagmail

Next we import our required modules. All we need outside of core Python is requests, to make the API call and yagmail which will send the email alert if the form submission fails.

import requests
import yagmail

Crafting the API Call

Let’s set a few variables we’ll need for the API call. First replace YOUR_DOMAIN_PATH with your own domain complete with HTTP/S. The number, which is currently 2, between forms and submissions in the path must match with the Gravity Form ID you are submitting to. This can be found while editing the form in WordPress.

url = "YOUR_DOMAIN_PATH/wp-json/gf/v2/forms/2/submissions"

Second we need a payload variable populated with the data we want the server to process. In this instance it’s the form field data. Each input name must match with how it is named in the form. This can all be found while editing a form in WordPress. Just follow this format while adding or removing depending what fields you have.

payload = "{\r\n \"input_1\": \"FormBot\",\r\n \"input_2\": \"formtest@success.com\",\r\n \"input_4\": \"222-222-2222\",\r\n \"field_values\": \"\",\r\n \"source_page\": 1,\r\n \"target_page\": 0\r\n}"

Third, we add a headers variable which contains your API key, content type and a tracking cookie. Be sure to replace “YOUR_API_KEY”. The key can be found in the Gravity Forms settings page in WordPress admin.

headers = {
    'Authorization': 'Basic YOUR_API_KEY',
    'Content-Type': 'application/json',
    'Cookie': 'ct_traffic_source_cookie=NOBASE64-%7B%22traffic_source%22%3A%22Direct%22%7D'
}

With our API request formatted the the above variables, let’s make the call. This request will be a POST since we are pushing data to the server.

Send API Call

response = requests.request("POST", url, headers=headers, data = payload)

Create Notification

We’ll know if the form works because we’ll see the entry and likely a notification email from Gravity Forms. Now we’d like some notification if the form fails. Rather than wondering what happened and why we haven’t had a new submission in days. First we test if the response from Gravity Forms API is anything other than a valid 200. If it is, great, just print a console message confirming. If not 200, then we have an error and we need to notify.

Use the body variable to craft your message. I’ve added the error message for further debugging. Be sure to change YOUR_GMAIL_ADDRESS with the one you configured with yagmail earlier. Then replace TO_GMAIL_ADDRESS with the email address that should be receiving the notification.

if response.status_code != 200:
    body = "Form Submission Test Failed. Error captured below:\n\n"
    body += str(response.text.encode('utf8'))
    yag = yagmail.SMTP("YOUR_GMAIL_ADDRESS")
    yag.send(
    to="TO_EMAIL_ADDRESS",
    subject="Gravity Form Submission Failed",
    contents=body
    )
    print("api failed, email sent")
    print(response.text.encode('utf8'))
else:
    print("api worked, form sent")

Conclusion

You can easily automate this script to run every morning via cronjob and or store success rates in a database.  Sleep well knowing your Gravity Forms are working as expected and you won’t be losing any more leads. Feel free to comment or Tweet me any cool ways you’ve built Gravity Form API into your own scripts and extended this one.

Greg Bernhardt
Follow me

Leave a Reply