python gravity forms api

Submit a WordPress Gravity Form via API with Python

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

Gravity Forms is a popular WordPress form plugin. If you run a leads-based business, you need to know your form is working and that prospects can contact you. We’ve all looked at the entry log and noticed an unusual gap between the last entry and the present — maybe there was a glitch. How would you know? In this tutorial, I’ll show you how to submit a form using the Gravity Forms free API with Python.

Feel free to browse the Gravity Forms API documentation before we begin. Let’s get started!

Requirements and Assumptions

  • Python 3 is installed and basic Python syntax is understood
  • Access to a Linux installation (I recommend Ubuntu)
  • WordPress Gravity Forms installed in your site
  • yagmail installed and configured

Starting the Script

First, let’s install and import the yagmail package so we can send an email.

pip3 install yagmail

Next, import the required modules. Outside of core Python you only need requests to make the API call and yagmail to send an email alert if the form submission fails.

import requests
import yagmail

Crafting the Gravity Form API Call

Set a few variables for the API call. First, replace YOUR_DOMAIN_PATH with your site’s domain including http:// or https://. The number (currently 2) in the path between forms and submissions must match the Gravity Form ID you are submitting to; you can find this while editing the form in WordPress.

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

Next, populate a payload variable with the form field data you want the server to process. Each input name must match the names used in your form — you can find these when editing the form in WordPress. Add or remove fields to match your form’s structure.

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}"

Then add a headers variable containing your API key, content type, and a tracking cookie. Replace YOUR_API_KEY with the key found in Gravity Forms → Settings in the 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 the variables set, make the API call. Use POST because you’re sending data to the server.

Send Gravity Form API Call

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

Create Notification

You can verify the form works by seeing the entry in Gravity Forms and often receiving a notification email. We also want an automated alert if the form submission fails so we don’t have to wonder why there have been no recent submissions. Check the API response status: if it’s 200, print a confirmation to the console; if it’s not 200, send a notification.

Use the body variable to craft the notification message. The example includes the error text for debugging. Replace YOUR_GMAIL_ADDRESS with the Gmail address configured for yagmail and TO_GMAIL_ADDRESS with the recipient address for notifications.

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 automate this script to run daily with a cronjob or record success rates in a database. Sleep well knowing your Gravity Forms are working and you won’t lose leads. Try it out! Follow me on Twitter and share your Gravity Forms 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, use the WordPress REST API. Send a POST request to the Gravity Forms submissions endpoint and include 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 and is suitable for interacting with the WordPress REST API. Include this library in your script to send API requests.

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

Yes, authentication is required. Obtain an authentication token—commonly a JSON Web Token (JWT)—by authenticating with the WordPress site, and 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 accept 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. Community resources and forums also provide examples and tutorials on using Python to interact with the WordPress API for Gravity Form submissions.

Greg Bernhardt
Follow me