python gravity forms api
Estimated Read Time: 4 minute(s)
Common Topics: form, api, gravity, wordpress, data

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!

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 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")
    subject="Gravity Form Submission Failed",
    print("api failed, email sent")
    print("api worked, form sent")


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!

Python and Gravity Forms API FAQ

How can I submit a WordPress Gravity Form via API using Python?

To submit a WordPress Gravity Form programmatically with Python, you can use the WordPress REST API. Send a POST request to the endpoint corresponding to the Gravity Forms entry point, providing the necessary form data in the request payload.

What Python libraries are commonly used for interacting with the WordPress REST API?

The requests library is commonly used in Python for making HTTP requests, making it suitable for interacting with the WordPress REST API. Include this library in your Python script to send API requests seamlessly.

Are there specific authentication requirements for submitting a Gravity Form via the WordPress API?

Yes, authentication is required. Obtain an authentication token, usually in the form of a JWT (JSON Web Token), by authenticating with the WordPress site. Include this token in the headers of your API request to ensure proper authentication.

Can I handle file uploads when submitting a Gravity Form via the API with Python?

Yes, you can handle file uploads by including the file data in your API request payload. Ensure that your form and the WordPress site are configured to handle file uploads, and include the file data in your Python script when making the API request.

Where can I find documentation and examples for submitting Gravity Forms via the WordPress API with Python?

Comprehensive documentation for the WordPress REST API is available on the official WordPress website. Additionally, various online resources and forums provide examples and tutorials on using Python to interact with the WordPress API for Gravity Form submissions.

Greg Bernhardt
Follow me

Leave a Reply