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? In this tutorial, I’m going to show you how to submit a form using Gravity Forms free API via Python.
Feel free to browse the Gravity Form API documentation before we begin. Let’s get started!
Table of Contents
Requirements and Assumptions
- Python 3 is installed and basic Python syntax understood
- Access to a Linux installation (I recommend Ubuntu)
- WordPress Gravity Forms installed
- yagmail installed and configured
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 Gravity Form 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 on what fields you have.
payload = "{\r\n \"input_1\": \"FormBot\",\r\n \"input_2\": \"[email protected]\",\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 that 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 above variables, let’s make the call. This request will be a POST since we are pushing data to the server.
Send Gravity Form 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. Now get out there and try it out! Follow me on Twitter and let me know your Gravity Form API applications and ideas!
- Calculate SERP Rank Readability Scores Using Python - August 20, 2023
- Find Interlinking Opps via Entity N-gram Matches Using Python - April 3, 2023
- Build and Run Python Scripts on the Fly With GPT-3 - January 5, 2023