This article includes more detailed examples of how to interact with the REST API v1, formerly called the Web API. It is a work in progress. More examples will be added over time.
Entries
Retrieve Entries
Retrieve the latest 10 entries for a form
The example below retrieves the 10 most recent entries for form id 28. Paging is set to 10 by default.
PHP
public static function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } $base_url = 'http://localhost/wpdev/gravityformsapi/'; $api_key = 'your_api_key'; $private_key = 'your_private_key'; $method = 'GET'; $route = 'forms/28/entries'; $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, $method, $route, $expires ); $sig = self::calculate_signature( $string_to_sign, $private_key ); $url = $base_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires; $response = wp_remote_request( $url, array('method' => 'GET' ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed echo 'There was an error attempting to access the API.'; die(); } $body_json = wp_remote_retrieve_body( $response ); //results are in the "body" and are json encoded, decode them and put into an array $body = json_decode( $body_json, true ); $data = $body['response']; $status_code = $body['status']; $total = 0; $total_retrieved = 0; if ( $status_code <= 202 ){ //entries retrieved successfully $entries = $data['entries']; $status = $status_code; $total = $data['total_count']; $total_retrieved = count( $entries ); } else { //entry retrieval failed, get error information $error_code = $data['code']; $error_message = $data['message']; $error_data = isset( $data['data'] ) ? $data['data'] : ''; $status = $status_code . ' - ' . $error_code . ' ' . $error_message . ' ' . $error_data; } //display results in a simple page ?> <html> <body> <form> <p>Results</p> <div>Status Code: <?php echo $status ?></div> <div>Total Count: <?php echo $total; ?></div> <div>Total Retrieved: <?php echo $total_retrieved; ?></div> <div>JSON Response:<br/><textarea style="vertical-align: top" cols="125" rows="10"> <?php echo $response['body']; ?></textarea></div> <br/> <div> <?php if ( $total_retrieved > 0 ) { echo '<table border="1"><th>Form ID</th><th>Entry ID</th><th>Date Created</th>'; foreach ( $entries as $entry ){ echo '<tr><td>' . $entry['form_id'] . '</td><td>' . $entry['id'] . '</td><td>' . $entry['date_created'] . '</td></tr>'; } echo '</table>'; } ?> </div> </form> </body> </html>
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; var method = "GET"; var route = "forms/1/entries"; stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var url = 'http://localhost/wp.dev/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; $.get(url, function(data, textStatus) { //get the data from the api if ( data.status != 200 || ( typeof( data ) != 'object' ) ) { //http request failed document.write( 'There was an error attempting to access the API - ' + data.status + ': ' + data.response ); return; } response = data.response; entries = response.entries; //entries is a collection of Entry objects total_count = response.total_count; }); </script>
The response from this example will look like the following if successful:
{"status":200,"response":{"total_count":"22","entries":[{"id":"171","form_id":"28","date_created":"2015-06-15 22:43:23","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":"","is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"active","1.3":"Manual","1.6":"Creation2","2":"create entry x","3":"create entry 2","1.2":"","1.4":"","1.8":""},{"id":"170","form_id":"28","date_created":"2015-06-15 22:43:23","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":"","is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"active","1.3":"Manual","1.6":"Creation1","2":"create entry x","3":"create entry with form id specified","1.2":"","1.4":"","1.8":""},{"id":"169","form_id":"28","date_created":"2015-06-15 22:43:23","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":"","is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"active","1.3":"Manual","1.6":"Creation2","2":"create entry 2","3":"create entry 2","1.2":"","1.4":"","1.8":""},{"id":"168","form_id":"28","date_created":"2015-06-15 22:43:23","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":"","is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"active","1.3":"Manual","1.6":"Creation1","2":"create entry","3":"create entry with form id specified","1.2":"","1.4":"","1.8":""},{"id":"167","form_id":"28","date_created":"2015-06-15 22:43:23","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":"","is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"active","1.3":"Manual","1.6":"Creation2","2":"create entry 2","3":"create entry 2","1.2":"","1.4":"","1.8":""},{"id":"166","form_id":"28","date_created":"2015-06-15 22:43:23","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":"","is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"active","1.3":"Manual","1.6":"Creation1","2":"create entry","3":"create entry with form id specified","1.2":"","1.4":"","1.8":""},{"id":"165","form_id":"28","date_created":"2015-06-15 22:43:23","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":"","is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"active","1.3":"Manual","1.6":"Creation2","2":"create entry 2","3":"create entry 2","1.2":"","1.4":"","1.8":""},{"id":"164","form_id":"28","date_created":"2015-06-15 22:43:23","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":"","is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"active","1.3":"Manual","1.6":"Creation1","2":"create entry","3":"create entry with form id specified","1.2":"","1.4":"","1.8":""},{"id":"163","form_id":"28","date_created":"2015-07-01 20:03:09","is_starred":0,"is_read":1,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":null,"is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"spam","1.3":"July","1.6":"First","2":"test","1.2":"","1.4":"","1.8":"","3":""},{"id":"162","form_id":"28","date_created":"2015-06-24 20:01:31","is_starred":0,"is_read":0,"ip":"::1","source_url":"http:\/\/localhost\/wpdev\/?gf_page=preview&id=28","post_id":null,"currency":"USD","payment_status":null,"payment_date":null,"transaction_id":null,"payment_amount":null,"payment_method":null,"is_fulfilled":null,"created_by":"1","transaction_type":null,"user_agent":"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko\/20100101 Firefox\/38.0","status":"trash","1.3":"d5","1.6":"d5","2":"d5","1.2":"","1.4":"","1.8":"","3":""}]}}
If no entries are located for the specified form, the response will look like the following:
{"status":200,"response":{"total_count":"0","entries":[]}}
Retrieve all entries for a form
To retrieve all entries for a form, you may specify the page size within the URL’s querystring. To do this, you may use the example above and add the querystring parameter “&paging[page_size]=” to the URL as in the example below. In this example, the size is set to 1000 since this form has very few entries. Because you may not have an idea of how many entries exist for a form, you could simply set the size to a very large number, or even run a query to get the entry count and then set the page_size greater than the count. To see more information about paging, check out the Paging section in the REST API v1 article.
http://localhost/wpdev/gravityformsapi/forms/28/entries/?paging[page_size]=1000
Retrieve entries sorted by a specific field
To retrieve entries and have them sorted by a specific field, you may specify the sorting key within the URL’s querystring. In the example below, field id 1.3 (first name field on this form) is used as the sort key. You may use field ids, or the set of available entry meta keys, like date_created, payment_status, created_by, etc. To see more information about sorting and which entry meta keys may be used, check out the Sorting section in the REST API v1 article.
http://localhost/wpdev/gravityformsapi/forms/28/entries/?sorting[key]=1.3
Retrieve entries sorted ascending
To retrieve entries and have them sorted ascending, instead of the default descending, you may specify the sorting direction within the URL’s querystring. In the example below, the direction is set to “ASC” for ascending. The results will be sorted by field id 1.3 ascending. In this case, by first name alphabetically. To see more information about sorting, check out the Sorting section in the REST API v1 article.
http://localhost/wpdev/gravityformsapi/forms/28/entries/?sorting[direction]=ASC
Retrieve entries by status
The example below retrieves entries that are currently in the trash. To see more information about retrieving entries with search criteria, check out the Search Criteria section the REST API v1 article.
http://localhost/wpdev/gravityformsapi/forms/28/entries/?search[status]=trash
Retrieve entries more recent than a specific date
The example below retrieves entries which have been created on 06/24/2014 or are newer.
http://localhost/wpdev/gravityformsapi/forms/28/entries/?search[start_date]=2015-06-24
Retrieve entries for a date range
The example below retrieves entries that were created on 06/15/2015 through 06/24/2015 (inclusive).
http://localhost/wpdev/gravityformsapi/forms/28/entries/?search[start_date]=2015-06-15&search[end_date]=2015-06-24
Retrieve entries using field filters (one condition)
The example below retrieves entries where the last name field (id 1.6) is “Draven”. The search field filters must be a JSON string. Create your array of search criteria, JSON-encode it, and then URL encode it. To see more information about using field filters, check out the Field Filters section in the REST API v1 article
PHP
//create the field filter array with key, operator, value and place it inside another array $field_filters = array ( array( 'key' => '1.6', 'operator' => 'is', 'value' => 'Draven' ) ); //set field_filters to array $search['field_filters'] = $field_filters; //convert the array to a JSON string and url encode it so the JSON formatting persists $search_json = urlencode( json_encode( $search ) ); //include field filters in search querystring parameter $url = $base_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires . '&paging[page_size]=1000&search=' . $search_json;
JavaScript
var search = { field_filters : [ { key: '2.6', value: 'Harris', operator: 'is' } ] }; //convert to a JSON string and url encode it so the JSON formatting persists search = encodeURI(JSON.stringify(search)); //add search to url url += '&search=' + search;
The JSON string created by the code above and used in the querystring will look like the following before it is URL encoded:
{"field_filters":[{"key":"1.6","operator":"is","value":"Draven"}]}
Retrieve entries using field filters (multiple conditions)
The example below retrieves entries where the last name field (1.6) is “Draven” and the first name field (1.3) is not “Eric”. If all conditions specified need to be met, the “mode” is set to “all” (the keyword “AND” is used in the database query). If all conditions are not necessary, set the “mode” to “any” (the keyword “OR” is used in the database query). The default mode is “all” so you do not need to include it when all conditions are required. To see more information about using field filters, check out the Field Filters section in the REST API v1 article.
PHP
$field_filters = array ( 'mode' => 'any', array( 'key' => '1.6', 'operator' => 'is', 'value' => 'Draven' ), array( 'key' => '1.3', 'operator' => 'isnot', 'value' => 'Eric' ), ); $search['field_filters'] = $field_filters; $search_json = urlencode( json_encode( $search ) ); $url = $base_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires . '&paging[page_size]=1000&search=' . $search_json;
JavaScript
var search = { field_filters : [ { key: '2.6', value: 'Harris', operator: 'is' }, { key: '2.3', value:'Xander', operator: 'is' } ] }; //convert to a JSON string and url encode it so the JSON formatting persists search = encodeURI(JSON.stringify(search)); //add search to url url += '&search=' + search;
The JSON string created by the code above and used in the querystring will look like the following before it is URL encoded:
{"field_filters":{"mode":"any","0":{"key":"1.6","operator":"is","value":"Draven"},"1":{"key":"1.3","operator":"isnot","value":"Eric"}}}
Retrieve entries using field filters (contains condition)
The example below retrieves entries that have the text “er” as part of the last name field (id 1.6).
PHP
$field_filters = array ( array( 'key' => '1.6', 'operator' => 'contains', 'value' => 'er' ), ); $search['field_filters'] = $field_filters; $search_json = urlencode( json_encode( $search ) ); $url = $base_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires . '&paging[page_size]=1000&search=' . $search_json;
JavaScript
var search = { field_filters : [ { key: '2.6', operator: 'contains', value: 'er' } ] }; //convert to a JSON string and url encode it so the JSON formatting persists search = encodeURI(JSON.stringify(search)); //add search to url url += '&search=' + search;
The JSON string created by the code above and used in the querystring will look like the following before it is URL encoded:
{"field_filters":[{"key":"1.6","operator":"contains","value":"er"}]}
Retrieve entries using search criteria and field filters
The example below retrieves entries that have the text “Test” as part of the last name field (id 1.6) and have been created since 2016-10-10
PHP
$field_filters = array ( array( 'key' => '1.6', 'operator' => 'contains', 'value' => 'Test' ), ); $search = []; $search['field_filters'] = $field_filters; $search['start_date'] = '2016-10-10'; $search_json = urlencode( json_encode( $search ) ); $url .= '&search=' . $search_json;
JavaScript
var search = { field_filters : [ { key: '1.6', operator: 'contains', value: 'Test' } ], start_date : '2016-10-10' }; //convert to a JSON string and url encode it so the JSON formatting persists search = encodeURI(JSON.stringify(search)); //add search to url url += '&search=' + search;
The JSON string created by the code above and used in the querystring will look like the following before it is URL encoded:
{"field_filters":[{"key":"1.6","operator":"contains","value":"Test"}],"start_date":"2016-10-10"}
Retrieve entries that have been read
PHP
$field_filters = array ( array( 'key' => 'is_read', 'operator' => 'is', 'value' => 1 ), ); $search['field_filters'] = $field_filters; $search_json = urlencode( json_encode( $search ) ); $url = $base_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires . '&paging[page_size]=1000&search=' . $search_json;
JavaScript
var search = { field_filters : [ { key: 'is_read', operator: 'is', value: 1 } ] }; //convert to a JSON string and url encode it so the JSON formatting persists search = encodeURI(JSON.stringify(search)); //add search to url url += '&search=' + search;
Retrieve all entries with paging
The example below is a basic use of paging with previous/next links. The page size is set to 5 and the offset is used to control which results are displayed as you navigate through the pages. To see more information about paging, see the Paging section of the REST API v1 article.
PHP
public static function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } $base_url = 'http://localhost/wpdev/gravityformsapi/'; $api_key = '9419b823a1'; $private_key = '4d7640ad3ffe2ec'; $method = 'GET'; $route = 'entries'; $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, $method, $route, $expires ); $sig = self::calculate_signature( $string_to_sign, $private_key ); $page_size = 5; $offset = 0; if ( isset( $_GET['paging']['offset'] ) ){ $offset = $_GET['paging']['offset']; } $url = $base_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires . '&paging[$page_size]=' . $page_size . '&paging[offset]=' . $offset; $response = wp_remote_request( $url, array('method' => 'GET' ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed echo 'There was an error attempting to access the API.'; die(); } $body_json = wp_remote_retrieve_body( $response ); //results are in the "body" and are json encoded, decode them and put into an array $body = json_decode( $body_json, true ); $data = $body['response']; $status_code = $body['status']; $total = 0; $total_retrieved = 0; if ( $status_code <= 202 ){ //entries retrieved successfully $entries = $data['entries']; $status = $status_code; $total = $data['total_count']; $total_retrieved = count( $entries ); } else { //entry retrieval failed, get error information $error_code = $data['code']; $error_message = $data['message']; $error_data = isset( $data['data'] ) ? $data['data'] : ''; $status = $status_code . ' - ' . $error_code . ' ' . $error_message . ' ' . $error_data; } //display results in a simple page ?> <html> <body> <form> <p>Results</p> <div>Status Code: <?php echo $status ?></div> <div>Total Count: <?php echo $total; ?></div> <div>Total Retrieved: <?php echo $total_retrieved; ?></div> <div>JSON Response:<br/><textarea style="vertical-align: top" cols="125" rows="10"> <?php echo $response['body']; ?></textarea></div> <br/> <div> <?php if ( $total_retrieved > 0 ) { echo '<table border="1"><th>Form ID</th><th>Entry ID</th><th>Date Created</th>'; foreach ( $entries as $entry ){ echo '<tr><td>' . $entry['form_id'] . '</td><td>' . $entry['id'] . '</td><td>' . $entry['date_created'] . '</td></tr>'; } echo '</table>'; } if ( $total > $total_retrieved ){ //paging in effect $query_string = $_SERVER['QUERY_STRING']; parse_str( $query_string, $params ); $paging_link = ''; if ( $total_retrieved == $page_size ){ //see if previous link needs to be built $page_offset = isset( $params['paging']['offset'] ) ? $params['paging']['offset'] : 0; if ( $page_offset <> 0 ) { $previous_page_offset = $params['paging']['offset'] - $page_size; if ( $previous_page_offset < 0 ){ $previous_page_offset = 0; } $params['paging']['offset'] = $previous_page_offset; $page_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?' . http_build_query( $params ); $paging_link = '<a href="' . $page_url . '">Previous</a> '; } $params['paging']['offset'] = $page_offset + $page_size; $page_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?' . http_build_query( $params ); $paging_link .= '<a href="' . $page_url . '">Next</a>'; } else { $page_offset = $params['paging']['offset'] - $page_size; if ( $page_offset < 0 ){ $page_offset = 0; } $params['paging']['offset'] = $page_offset; $page_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?' . http_build_query( $params ); $paging_link = '<a href="' . $page_url . '">Previous</a>'; } echo $paging_link; } ?> </div> </form> </body> </html>
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; var method = "GET"; var route = "forms/1/entries"; var page_size = 5; stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var url = 'http://localhost/wp.dev/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; url += '&paging[page_size]=' + page_size; $.get(url, function(data, textStatus) { //get the data from the api if ( data.status != 200 || ( typeof( data ) != 'object' ) ) { //http request failed document.write( 'There was an error attempting to access the API - ' + data.status + ': ' + data.response ); return; } response = data.response; entries = response.entries; //entries is a collection of Entry objects total_count = response.total_count; }); </script>
Retrieve a single entry
This example may be viewed in the REST API v1 article.
Retrieve specific fields for a single entry
This example may be viewed in the REST API v1 article.
Retrieve specific fields for multiple entries
This example may be viewed in the REST API v1 article.
Create Entries
Create Entries
This example may be viewed in the REST API v1 article.
Create Entry for a Specific Form
This example may be viewed in the REST API v1 article.
Update Entries
Update Single Entry (Create Entry Array Manually)
The example below updates entry id 157.
PHP
public static function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } $base_url = 'http://localhost/wpdev/gravityformsapi/'; $api_key = 'your_api_key'; $private_key = 'your_private_key'; $method = 'PUT'; $route = 'entries/157'; $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, $method, $route, $expires ); $sig = self::calculate_signature( $string_to_sign, $private_key ); $url = $base_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires; $entry = array( 'form_id' => '28', 'date_created' => '2015-06-15 22:43:23', 'is_starred' => 0, 'is_read' => 1, 'ip' => '::1', 'source_url' => 'http://localhost/wpdev/?gf_page=preview&id=28', 'currency' => 'USD', 'created_by' => 1, 'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', 'status' => 'active', '1.3' => 'Eric', '1.6' => 'Draven', '2' => 'brandonlee', '3' => 'The Crows', ); $entry_json = json_encode( $entry ); $response = wp_remote_request( $url, array( 'method' => $method, 'body' => $entry_json ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed echo 'There was an error attempting to access the API.'; die(); } $body_json = wp_remote_retrieve_body( $response ); //results are in the "body" and are json encoded, decode them and put into an array $body = json_decode( $body_json, true ); $data = $body['response']; $status_code = $body['status']; if ( $status_code <= 202 ){ //entries retrieved successfully $status = $status_code; } else { //entry update failed, get error information $error_code = $data['code']; $error_message = $data['message']; $error_data = isset( $data['data'] ) ? $data['data'] : ''; $status = $status_code . ' - ' . $error_code . ' ' . $error_message . ' ' . $error_data; } //display results in a simple page ?> <html> <body> <form> <p>Results</p> <div>Status Code: <?php echo $status ?></div> <div>JSON Response:<br/><textarea style="vertical-align: top" cols="125" rows="10"> <?php echo $response['body']; ?></textarea></div> </form> </body> </html>
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; var method = "PUT"; var route = "entries/157"; stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var url = 'http://localhost/wp.dev/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; var update_entry = { form_id : '28', date_created: '2016-07-29 21:38:23', is_starred : 0, is_read : 1, ip : '::1', source_url : 'http://localhost/wpdev/?gf_page=preview&id=1', currency : 'USD', created_by : 1, user_agent : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', status : 'active', 1.3 : 'Testing', 1.6 : 'Tester', 3 : 'The Crowsxxxxxxxxxxxxx' } ; entry_json = JSON.stringify( update_entry ); $.ajax({ url: url, type: 'PUT', data: entry_json, contentType:'application/json', success: function(result) {alert('success');}, error: function(result, textStatus, errorThrown){alert('error ' + errorThrown);} }); </script>
Update Single Entry (Create Entry Array Using Existing Database Record)
This example may be viewed in the REST API v1 article.
Update Entries (Create Entries Array Manually)
This example may be viewed in the REST API v1 article.
Update Entries (Create Entries Array Using Existing Database Records)
PHP
public static function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } $base_url = 'http://localhost/wpdev/gravityformsapi/'; $api_key = 'your_api_key'; $private_key = 'your_private_key'; $method = 'PUT'; $route = 'entries'; $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, $method, $route, $expires ); $sig = self::calculate_signature( $string_to_sign, $private_key ); $url = $base_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires; //get entry object for id 156 and modify $entry1 = GFAPI::get_entry( 156 ); $entry1['is_starred'] = 1; $entry1['3'] = 'Newt'; //add entry to entries array $entries[] = $entry1; //get entry object for id 157 and modify $entry2 = GFAPI::get_entry( 157 ); $entry2['2'] = 'eric draven'; $entry2['is_read'] = 0; //add entry to entries array $entries[] = $entry2; //json encode array $entry_json = json_encode( $entries ); $response = wp_remote_request( $url, array( 'method' => $method, 'body' => $entry_json ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed echo 'There was an error attempting to access the API.'; die(); } $body_json = wp_remote_retrieve_body( $response ); //results are in the "body" and are json encoded, decode them and put into an array $body = json_decode( $body_json, true ); $data = $body['response']; $status_code = $body['status']; if ( $status_code <= 202 ){ //entries retrieved successfully $status = $status_code; } else { //entry update failed, get error information, error could just be a string if ( is_array( $data )){ $error_code = $data['code']; $error_message = $data['message']; $error_data = isset( $data['data'] ) ? $data['data'] : ''; $status = $status_code . ' - ' . $error_code . ' ' . $error_message . ' ' . $error_data; } else{ $status = $data; } } //display results in a simple page ?> <html> <body> <form> <p>Results</p> <div>Status Code: <?php echo $status ?></div> <div>JSON Response:<br/><textarea style="vertical-align: top" cols="125" rows="10"> <?php echo $response['body']; ?></textarea></div> </form> </body> </html>
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; //create signature and url for putting entries stringToSign = publicKey + ":PUT:entries:" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var put_url = 'http://localhost/wp.dev/gravityformsapi/entries?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; //create signature and url for first entry stringToSign = publicKey + ":GET:entries/7:" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var get_url1 = 'http://localhost/wp.dev/gravityformsapi/entries/7?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; //get signature and url for second entry stringToSign = publicKey + ":GET:entries/16:" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var get_url2 = 'http://localhost/wp.dev/gravityformsapi/entries/16?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; $.get(get_url1, function(data, textStatus) { //get the data from the api for the first entry if ( data.status != 200 || ( typeof( data ) != 'object' ) ) { //http request failed document.write( 'There was an error attempting to access the API - ' + data.status + ': ' + data.response ); return; } entry1 = data.response; entry1['is_starred'] = 1; entry1['2.3'] = 'Newt'; entry1['2.6'] = 'Alien'; $.get(get_url2, function(data, textStatus){ //get the data for the second entry entry2 = data.response; entry2['is_read'] = 0; entry2['1'] = 'first field data'; entry2['2.3'] = 'New'; entry2['2.6'] = 'Name'; var entries = Array(); entries[0] = entry1; entries[1] = entry2; entry_json = JSON.stringify(entries); //update the entries $.ajax({ url: put_url, type: 'PUT', data: entry_json, contentType:'application/json', success: function(result) {alert('success');}, error: function(result, textStatus, errorThrown){alert('error ' + errorThrown);} }); }); }); </script>
Delete Entries
Delete Multiple Entries
This example may be viewed in the REST API v1 article.
Forms
Retrieve Forms
Retrieve all active forms
This example retrieves all active forms and displays them in a list.
PHP
function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } //set API keys $api_key = 'your_api_key'; $private_key = 'your_private_key'; //set route $route = 'forms'; //creating request URL $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, 'GET', $route, $expires ); $sig = self::calculate_signature( $string_to_sign, $private_key ); $url = 'http://localhost/wpdev/gravityformsapi/' . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires; //retrieve data $response = wp_remote_request( $url, array( 'method' => 'GET' ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed die( 'There was an error attempting to access the API.' ); } //result is in the response "body" and is json encoded. $body = json_decode( wp_remote_retrieve_body( $response ), true ); if( $body['status'] > 202 ){ die( "Could not retrieve forms." ); } //forms retrieved successfully $forms = $body['response']; //display results in a simple page ?> <html> <body> <form> <p>Forms</p> <div> <?php if ( $forms ) { echo '<table border="1"><th>Form ID</th><th>Form Title</th><th>Entry Count</th>'; foreach ( $forms as $form ) { echo '<tr><td>' . $form['id'] . '</td><td>' . $form['title'] . '</td><td>' . $form['entries'] . '</td></tr>'; } echo '</table>'; } ?> </div> <br/> <div>JSON Response:<br/><textarea style="vertical-align: top" cols="125" rows="10"> <?php echo $response['body']; ?></textarea></div> </form> </body> </html>
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; var method = "GET"; var route = "forms"; stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var url = 'http://localhost/wp.dev/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; $.get(url, function(data, textStatus) { //get the data from the api if ( data.status != 200 || ( typeof( data ) != 'object' ) ) { //http request failed document.write( 'There was an error attempting to access the API - ' + data.status + ': ' + data.response ); return; } forms = data.response; document.write('<p>Results</p>'); document.write('<table border="1"><th>Form ID</th><th>Form Title</th><th>Entry Count</th>'); for (var form in forms) { document.write('<tr><td>' + forms[form]['id'] + '</td><td>' + forms[form]['title'] + '</td><td>' + forms[form]['entries'] + '</td></tr>'); } document.write("</table>"); }); </script>
Retrieve a single form
This example retrieves a single form and displays the details, along with field information.
PHP
function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } //set API keys $api_key = 'your_api_key'; $private_key = 'your_private_key'; //set route $route = 'forms/67'; //creating request URL $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, 'GET', $route, $expires ); $sig = calculate_signature( $string_to_sign, $private_key ); $url = 'http://localhost/wp/gravityformsapi/' . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires; //retrieve data $response = wp_remote_request( $url, array( 'method' => 'GET' ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed die( 'There was an error attempting to access the API.' ); } //result is in the response "body" and is json encoded. $body = json_decode( wp_remote_retrieve_body( $response ), true ); if( $body['status'] > 202 ){ die( "Could not retrieve forms." ); } //forms retrieved successfully $form = $body['response']; //display results in a simple page ?> <html> <body> <form> <p>Forms</p> <div> <?php if ( $form ) { echo '<table border="1"><th>Form ID</th><th>Form Title</th><th>Field Count</th>'; //foreach ( $forms as $form ) { $fields = $form['fields']; echo '<tr><td>' . $form['id'] . '</td><td>' . $form['title'] . '</td><td>' . count( $fields ) . '</td></tr>'; if ( $fields ){ echo '<tr><td colspan="3"><table border="1"><th>Field ID</th><th>Field Label</th><th>Field Type</th>'; foreach ( $fields as $field ){ echo '<tr><td>' . $field['id'] . '</td><td>' . GFCommon::get_label( $field ) . '</td><td>' . $field['type'] . '</td></tr>'; } echo '</table></td></tr>'; } echo '<tr><td colspan="3"> </td></tr>'; //} echo '</table>'; } ?> </div> <br/> <div>JSON Response:<br/><textarea style="vertical-align: top" cols="125" rows="10"> <?php echo $response['body']; ?></textarea></div> </form> </body> </html>
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; var method = "GET"; var route = "forms/1"; stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var url = 'http://localhost/wp.dev/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; $.get(url, function(data, textStatus) { //get the data from the api if ( data.status != 200 || ( typeof( data ) != 'object' ) ) { //http request failed document.write( 'There was an error attempting to access the API - ' + data.status + ': ' + data.response ); return; } form = data.response; fields = form['fields']; field_count=fields['length']; document.write('<p>Results</p>'); document.write('<table border="1"><th>Form ID</th><th>Form Title</th><th>Field Count</th>'); document.write('<tr><td>' + form['id'] + '</td><td>' + form['title'] + '</td><td>' + field_count + '</td></tr>'); if ( fields ){ document.write('<tr><td colspan="3"><table border="1"><th>Field ID</th><th>Field Label</th><th>Field Type</th>'); for ( var i=0;i<field_count;i++ ){ document.write('<tr><td>' + fields[i]['id'] + '</td><td>' + fields[i]['label'] + '</td><td>' + fields[i]['type'] + '</td></tr>'); } document.write('</table></td></tr>'); } document.write('<tr><td colspan="3"> </td></tr>'); document.write("</table>"); }); </script>
Retrieve multiple forms
This example retrieves multiple forms and displays the details, along with field information. If a form cannot be found, false is returned instead of a form object.
PHP
function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } //set API keys $api_key = 'your_api_key'; $private_key = 'your_private_key'; //set route $route = 'forms/67;68;1'; //creating request URL $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, 'GET', $route, $expires ); $sig = calculate_signature( $string_to_sign, $private_key ); $url = 'http://localhost/wpdev/gravityformsapi/' . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires; //retrieve data $response = wp_remote_request( $url, array( 'method' => 'GET' ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed die( 'There was an error attempting to access the API.' ); } //result is in the response "body" and is json encoded. $body = json_decode( wp_remote_retrieve_body( $response ), true ); if( $body['status'] > 202 ){ die( "Could not retrieve forms." ); } //forms retrieved successfully $forms = $body['response']; //display results in a simple page ?> <html> <body> <form> <p>Forms</p> <div> <?php if ( $forms ) { echo '<table border="1"><th>Form ID</th><th>Form Title</th><th>Field Count</th>'; foreach ( $forms as $form_id => $form ) { if ( ! $form ){ //if a form is not found, false is returned echo '<tr><td colspan="3">Form ID ' . $form_id . ' could not be found.</td></tr>'; } else{ $fields = $form['fields']; echo '<tr><td>' . $form['id'] . '</td><td>' . $form['title'] . '</td><td>' . count( $fields ) . '</td></tr>'; if ( $fields ){ //display basic field info echo '<tr><td colspan="3"><table border="1"><th>Field ID</th><th>Field Label</th><th>Field Type</th>'; foreach ( $fields as $field ){ echo '<tr><td>' . $field['id'] . '</td><td>' . GFCommon::get_label( $field ) . '</td><td>' . $field['type'] . '</td></tr>'; } echo '</table></td></tr>'; } } echo '<tr><td colspan="3"> </td></tr>'; } echo '</table>'; } ?> </div> <br/> <div>JSON Response:<br/><textarea style="vertical-align: top" cols="125" rows="10"> <?php echo $response['body']; ?></textarea></div> </form> </body> </html>
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; var method = "GET"; var route = "forms/1;9;2;3;4"; stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var url = 'http://localhost/wp.dev/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; $.get(url, function(data, textStatus) { //get the data from the api if ( data.status != 200 || ( typeof( data ) != 'object' ) ) { //http request failed document.write( 'There was an error attempting to access the API - ' + data.status + ': ' + data.response ); return; } forms = data.response; if ( forms ){ document.write('<p>Results</p>'); document.write('<table border="1"><th>Form ID</th><th>Form Title</th><th>Field Count</th>'); //loop through form ids returned for (var id in forms){ //if a form is not found, false is returned if ( ! forms[id]){ document.write('<tr><td colspan="3">Form ID ' + id + ' could not be found.</td></tr>'); } else{ form = forms[id]; //get single form object using id fields = form['fields']; field_count=fields['length']; document.write('<tr><td>' + form['id'] + '</td><td>' + form['title'] + '</td><td>' + field_count + '</td></tr>'); if ( fields ){ document.write('<tr><td colspan="3"><table border="1"><th>Field ID</th><th>Field Label</th><th>Field Type</th>'); for ( var i=0;i<field_count;i++ ){ document.write('<tr><td>' + fields[i]['id'] + '</td><td>' + fields[i]['label'] + '</td><td>' + fields[i]['type'] + '</td></tr>'); } document.write('</table></td></tr>'); } document.write('<tr><td colspan="3"> </td></tr>'); } } document.write("</table>"); } }); </script>
Insert Forms
Insert a single form
The example below inserts a new form.
PHP
function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } //set API keys $api_key = 'your_api_key'; $private_key = 'your_private_key'; //set route $route = 'forms'; //creating request URL $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, 'POST', $route, $expires ); $sig = calculate_signature( $string_to_sign, $private_key ); $url = 'http://localhost/wpdev/gravityformsapi/' . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires; $form = array( array( 'title' => 'API Generated Form', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'confirmations' => array( array( 'id' => 0, 'name' => 'Default Confirmation', 'type' => 'message', 'message' => 'Thanks for contacting us! We will get in touch with you shortly.', 'isDefault' => true, ), ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ) ), ), ); //json encode array $form_json = json_encode( $form ); //retrieve data $response = wp_remote_request( $url, array( 'method' => 'POST', 'body' => $form_json ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed die( 'There was an error attempting to access the API.' ); } //result is in the response "body" and is json encoded. $body = json_decode( wp_remote_retrieve_body( $response ), true ); if( $body['status'] > 202 ){ $error = $body['response']; //form insert failed, get error information $error_code = $error['code']; $error_message = $error['message']; $error_data = isset( $error['data'] ) ? $error['data'] : ''; $status = "Code: {$error_code}. Message: {$error_message}. Data: {$error_data}."; die( "Could not post forms. {$status}" ); } $form_id = $body['response'][0]; echo 'The following form id was created: ' . $form_id . '</br>';
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; var method = "POST"; var route = "forms"; stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var url = 'http://localhost/wp.dev/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; var form = { title : 'API Generated Form', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : 0, name : 'Default Confirmation', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : true}, {id : 1, name : 'Confirmation 2', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : false} ], fields : [ {id : '1', label : 'My Text', type : 'text'}, {id : '2', label : 'More Text', type : 'text'} ] }; var form_array = []; form_array[0] = form; form_json = JSON.stringify(form_array); $.post(url, form_json, function(data){ console.log(data.response); }); </script>
Insert form with the confirmation set to redirect
The example below shows how to build the form array to have the confirmation as a redirect.
PHP
$form = array( array( 'title' => 'API Generated Form With Redirect Confirmation', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'confirmations' => array( array( 'id' => 0, 'name' => 'Default Confirmation', 'type' => 'redirect', 'url' => 'http://www.rocketgenius.com', 'isDefault' => true, ), ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ) ), ), );
JavaScript
var form = { title : 'API Generated Form With Redirect Confirmation', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : 0, name : 'Default Confirmation', type : 'redirect', url : 'http://www.rocketgenius.com', isDefault : true}, {id : 1, name : 'Confirmation 2', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : false} ], fields : [ {id : '1', label : 'My Text', type : 'text'}, {id : '2', label : 'More Text', type : 'text'} ] };
Insert form with the confirmation set to page
The example below shows how to build the form array to have the confirmation as a page.
PHP
$form = array( array( 'title' => 'API Generated Form with Page Confirmation', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'confirmations' => array( array( 'id' => 0, 'name' => 'Default Confirmation', 'type' => 'page', 'pageId' => 500, 'isDefault' => true, ), ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ) ), ), );
JavaScript
var form = { title : 'API Generated Form with Page Confirmation', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : 0, name : 'Default Confirmation', type : 'page', pageId : 2, isDefault : true} ], fields : [ {id : '1', label : 'My Text', type : 'text'}, {id : '2', label : 'More Text', type : 'text'} ] };
Insert form with a conditional confirmation
The example below shows how to build the form array to have a conditional confirmation.
PHP
$form = array( array( 'title' => 'API Generated Test Confirmations with new array 3', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'confirmations' => array( array( 'id' => 0, 'name' => 'Default Confirmation', 'type' => 'message', 'message' => 'Thanks for contacting us! We will get in touch with you shortly.', 'isDefault' => true, ), array( 'id' => 1, 'name' => 'My Conditional', 'type' => 'message', 'message' => 'This is my conditional confirmation that will be used if the text is test.', 'isDefault' => false, 'conditionalLogic' => array( 'actionType' => 'show', 'logicType' => 'any', 'rules' => array( array( 'fieldId' => 1, 'operator' => 'is', 'value' => 'test', ), array( 'fieldId' => 1, 'operator' => 'is', 'value' => 'TEST', ), array( 'fieldId' => 1, 'operator' => 'is', 'value' => 'Test', ), ), ), ) ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ) ), ), );
JavaScript
var form = { title : 'API Generated Test Confirmations Conditionals', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : '0', name : 'Default Confirmation', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : true}, {id : '1', name : 'My Conditional', type : 'message', message : 'This is my conditional confirmation that will be used if the text is test.', isDefault : false, conditionalLogic : {actionType : 'show', logicType : 'any', rules : [ {fieldId : '1', operator : 'is', value : 'test'}, {fieldId : '1', operator : 'is', value : 'TEST'}, {fieldId : '1', operator : 'is', value : 'Test'} ] } } ], fields : [ {id : '1', label : 'My Text', type : 'text'}, {id : '2', label : 'More Text', type : 'text'} ] };
Insert form with notifications sent to a specific email address
The example below shows how to build the form array to have notifications which are sent to a specific email address. Please see the article titled Notifications Object for more details about what may be included when building the notification array.
PHP
$form = array( array( 'title' => 'API Generated Form With Notification Sent to Specific Email Address', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'notifications' => array( array( 'id' => uniqid( '0' ), 'name' => 'Admin Notification', 'to' => '{admin_email}', 'toType' => 'email', 'event' => 'form_submission', 'subject' => 'New submission from {form_title}', 'message' => '{all_fields}', 'from' => '{admin_email}', 'fromName' => 'Administrator' ), array( 'id' => uniqid( '1' ), 'name' => 'Admin Notification 2', 'to' => '{admin_email}', 'toType' => 'email', 'event' => 'form_submission', 'subject' => 'New submission from {form_title}', 'message' => '{all_fields}', 'from' => '{admin_email}', 'isActive' => false, ), ), 'confirmations' => array( array( 'id' => 0, 'name' => 'Default Confirmation', 'type' => 'message', 'message' => 'Thanks for contacting us! We will get in touch with you shortly.', 'isDefault' => true, ), ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ) ), ), );
JavaScript
var form = { title : 'API Generated Form With Notification Sent to Specific Email Address', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : '0', name : 'Default Confirmation', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : true} ], fields : [ {id : '1', label : 'My Text', type : 'text'}, {id : '2', label : 'More Text', type : 'text'} ], notifications : [ {id : Math.floor(Math.random() * 1000), name : 'Admin Notification 1', to : '{admin_email}', toType : 'email', event : 'form_submission', subject : 'New submission from {form_title}', message : '{all_fields}', from : '{admin_email}', fromName : 'Administrator'}, {id : Math.floor(Math.random() * 1000), name : 'Admin Notification 2', to : '{admin_email}', toType : 'email', event : 'form_submission', subject : 'New submission from {form_title}', message : '{all_fields}', from : '{admin_email}', isActive : false} ] };
Insert form with notification set to use an email field
The example below shows how to build the form array to have a notification which is sent to a specific email address field. Please see the article titled Notifications Object for more details about what may be included when building the notification array.
PHP
$form = array( array( 'title' => 'API Generated Form With Notification Set to Field', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'notifications' => array( array( 'id' => uniqid( '0' ), 'name' => 'Admin Notification', 'to' => '2', 'toType' => 'field', 'event' => 'form_submission', 'subject' => 'New submission from {form_title}', 'message' => '{all_fields}', 'from' => '{admin_email}', 'fromName' => 'Administrator' ), ), 'confirmations' => array( array( 'id' => uniqid(), 'name' => 'Default Confirmation', 'type' => 'message', 'message' => 'Thanks for contacting us! We will get in touch with you shortly.', 'isDefault' => true, ), ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ), array( 'id' => '2', 'label' => 'Email', 'type' => 'email' ), ), ), );
JavaScript
var form = { title : 'API Generated Form With Notification Sent to Specific Email Address', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : '0', name : 'Default Confirmation', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : true} ], fields : [ {id : '1', label : 'My Text', type : 'text'}, {id : '2', label : 'More Text', type : 'text'}, {id : '3', label : 'Email', type : 'email'} ], notifications : [ {id : Math.floor(Math.random() * 1000), name : 'Admin Notification 1', to : '3', toType : 'field', event : 'form_submission', subject : 'New submission from {form_title}', message : '{all_fields}', from : '{admin_email}', fromName : 'Administrator'} ] };
Insert form with notification set to use routing
The example below shows how to build the form array to have a notification which is sent using routing rules. Please see the article titled Notifications Object for more details about what may be included when building the notification array.
PHP
$form = array( array( 'title' => 'API Generated Test Confirmations with new array 3', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'notifications' => array( array( 'id' => uniqid( '0' ), 'name' => 'Admin Notification', 'toType' => 'routing', 'event' => 'form_submission', 'subject' => 'New submission from {form_title}', 'message' => '{all_fields}', 'from' => '{admin_email}', 'fromName' => 'Administrator', 'isActive' => true, 'routing' => array( array( 'fieldId' => '1', 'operator' => 'is', 'value' => 'test', 'email' => '{admin_email}', ), array( 'fieldId' => '2', 'operator' => 'is', 'value' => '[email protected]', 'email' => '{admin_email}', ) ), ), ), 'confirmations' => array( array( 'id' => uniqid(), 'name' => 'Default Confirmation', 'type' => 'message', 'message' => 'Thanks for contacting us! We will get in touch with you shortly.', 'isDefault' => true, ), ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ), array( 'id' => '2', 'label' => 'Email', 'type' => 'email' ), ), ), );
JavaScript
var form = { title : 'API Generated Form With Notification Sent to Specific Email Address', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : '0', name : 'Default Confirmation', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : true} ], fields : [ {id : '1', label : 'My Text', type : 'text'}, {id : '2', label : 'More Text', type : 'text'}, {id : '3', label : 'Email', type : 'email'} ], notifications : [ {id : Math.floor(Math.random() * 1000), name : 'Admin Notification', toType : 'routing', event : 'form_submission', subject : 'New submission from {form_title}', message : '{all_fields}', from : '{admin_email}', fromName : 'Administrator', isActive : true, routing : [ {fieldId : '1', operator : 'is', value : 'test', email : '{admin_email}'}, {fieldId : '2', operator : 'is', value : '[email protected]', email : '{admin_email}'} ] } ] };
Insert multiple forms
The example below inserts two new forms and displays the ids of the forms inserted.
PHP
function calculate_signature( $string, $private_key ) { $hash = hash_hmac( 'sha1', $string, $private_key, true ); $sig = rawurlencode( base64_encode( $hash ) ); return $sig; } //set API keys $api_key = 'your_api_key'; $private_key = 'your_private_key'; //set route $route = 'forms'; //creating request URL $expires = strtotime( '+60 mins' ); $string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, 'POST', $route, $expires ); $sig = calculate_signature( $string_to_sign, $private_key ); $url = 'http://localhost/wpdev/gravityformsapi/' . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires; $form = array( array( 'title' => 'API Generated Form 1', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'confirmations' => array( array( 'id' => 0, 'name' => 'Default Confirmation', 'type' => 'message', 'message' => 'Thanks for contacting us! We will get in touch with you shortly.', 'isDefault' => true, ), ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ) ), ), array( 'title' => 'API Generated Form 2', 'description' => 'This is the description for the form generated by the API', 'labelPlacement' => 'top_label', 'button' => array( 'type' => 'text' ), 'confirmations' => array( array( 'id' => 0, 'name' => 'Default Confirmation', 'type' => 'message', 'message' => 'Thanks for contacting us! We will get in touch with you shortly.', 'isDefault' => true, ), ), 'fields' => array( array( 'id' => '1', 'label' => 'My Text', 'type' => 'text' ) ), ), ); //json encode array $form_json = json_encode( $form ); //retrieve data $response = wp_remote_request( $url, array( 'method' => 'POST', 'body' => $form_json ) ); if ( wp_remote_retrieve_response_code( $response ) != 200 || ( empty( wp_remote_retrieve_body( $response ) ) ) ){ //http request failed die( 'There was an error attempting to access the API.' ); } //result is in the response "body" and is json encoded. $body = json_decode( wp_remote_retrieve_body( $response ), true ); //echo $response['body']; if( $body['status'] > 202 ){ $error = $body['response']; //form insert failed, get error information $error_code = $error['code']; $error_message = $error['message']; $error_data = isset( $error['data'] ) ? $error['data'] : ''; $status = "Code: {$error_code}. Message: {$error_message}. Data: {$error_data}."; die( "Could not post forms. {$status}" ); } $form_ids = $body['response']; $form_ids_created = ''; foreach ( $form_ids as $form_id ){ $form_ids_created .= $form_id . '</br>'; } echo 'The following form ids were created: ' . $form_ids_created . '</br>';
JavaScript
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script> <script type="text/javascript"> function CalculateSig(stringToSign, privateKey){ //calculate the signature needed for authentication var hash = CryptoJS.HmacSHA1(stringToSign, privateKey); var base64 = hash.toString(CryptoJS.enc.Base64); return encodeURIComponent(base64); } //set variables var d = new Date; var expiration = 3600; // 1 hour, var unixtime = parseInt(d.getTime() / 1000); var future_unixtime = unixtime + expiration; var publicKey = "your_public_key"; var privateKey = "your_private_key"; var method = "POST"; var route = "forms"; stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime; sig = CalculateSig(stringToSign, privateKey); var url = 'http://localhost/wp.dev/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime; var forms = [ { title : 'API Generated Form 1', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : '0', name : 'Default Confirmation', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : true} ], fields : [ {id : '1', label : 'My Text', type : 'text'}, {id : '2', label : 'More Text', type : 'text'}, {id : '3', label : 'Email', type : 'email'} ], }, { title : 'API Generated Form 2', description : 'This is the description for the form generated by the API', labelPlacement : 'top_label', button : {type : 'text'}, confirmations : [ {id : '0', name : 'Default Confirmation', type : 'message', message : 'Thanks for contacting us! We will get in touch with you shortly.', isDefault : true} ], fields : [ {id : '1', label : 'My Text 2', type : 'text'}, {id : '2', label : 'More Text 2', type : 'text'}, {id : '3', label : 'Email 2', type : 'email'} ], } ]; form_json = JSON.stringify(forms); $.post(url, form_json, function(data){ document.write('Inserted Ids: ' + data.response); }); </script>
Update Forms
Update multiple forms
This example may be viewed in the REST API v1 article.
Delete Forms
This example may be viewed in the REST API v1 article.
Form Submissions
Complex Fields
Submit a list field that has a single column
This example shows how to create the input_values array for a list field (field 1) that has a single column. Each value in the array will be a separate row.
$values = array( 'input_values' => array( 'input_1' => array('row1','row2'), 'input_2' => 'test' ) );
Submit a list field that has multiple columns
This example shows how to create the input_values array for a list field (field 1) that has three columns. Each value in the array will fill up the columns for a row until starting on the columns for the next row. Use ” if there is no value for a column in a particular row. The value ‘test’ in the example starts a fourth row.
$values = array( 'input_values' => array( 'input_1' => array('row1col1','row1col2','row1col3','row2col1','row2col2','row3col3','row3col1','row3col2','row3col3','test'), ) );
Submit a Multi-Select Field
This example shows how to create the input_values array for a multi-select field (field 1).
$values = array( 'input_values' => array( 'input_1' => array('First Choice','Third Choice'), ) );
Pricing Fields
Single Product Field
This example shows how to create the input_values array for a Single Product Pricing field (field 1).
$values = array( 'input_values' => array( 'input_1_1' => 'test product', //product name 'input_1_2' => '$5.00', //product price with formatting 'input_1_3' => 1, //product quantity ) );
Single Product Field With Option
This examples shows how to create the input_values array for a product with an associated option (field id 2). The option needs to be in the format option name, pipe symbol, option price. This is the format whether the option is a drop down, check box, or radio button. When using check boxes, which allow multiple selections, you will need to reference the input fields a little differently. See the next example for this.
$values = array( 'input_values' => array( 'input_1_1' => 'test product', //product name 'input_1_2' => '$5.00', //product price with formatting 'input_1_3' => 1, //product quantity 'input_2' => 'Second Option|4' //option name|option price ) );
Option Field as a Checkbox
This example shows how to create the input_values array when the option associated to a product is a checkbox.
$values = array( 'input_values' => array( 'input_1_1' => 'test product', //product name 'input_1_2' => '$5.00', //product price with formatting 'input_1_3' => 1, //product quantity 'input_2_1' => 'First Option|2', //option name|option price 'input_2_3' => 'Third Option|6' ) );