Autopilot: Capture a form via API

If you have an advanced form that cannot be captured using Autopilot's form capture or Zapier integration, you can capture it via the Autopilot API. Please note, however, that Autopilot's form capture is compatible with most forms, so before capturing the form via API we highly recommend reviewing the form troubleshooting guide.

With the Autopilot tracking code loaded on the same page, a method from the Autopilot JavaScript library can be run to pull the contact’s

_autopilot_session_id

on the browser-side, and the API can pass the form fields and user association server-side. Please note that the API call must be made server-side, it cannot be sent from the browser.


Add the tracking code

Autopilot has two separate tracking codes, optimized for web pages and web apps.

Both load the Autopilot library and can be used for user association.

If the form is on your website (for example, a newsletter subscription form) then use the website tracking code. If it is in your web app (for example, a user preferences tab) then use the app tracking code.

Paste the code into the <head> tag of your web page or app’s HTML (depending on the use case).


Create any custom fields

User association allows you to pass any standard or custom field to Autopilot. Before you implement user association, check if the fields you are capturing are already included in Autopilot’s standard fields.

If they are not included, you will need to create new custom fields..


Implement user association

In your HTML, after the the form is submitted, pull the submitted fields and the visitor’s _autopilot_session_id and pass the data server-side to be transmitted securely via the encrypted Autopilot API calls.

If a form is submitted by a visitor who isn’t yet in Autopilot as a contact, we’ll automatically add them as a new contact. Existing users will have their data updated.

The call for pulling the session ID is:

var sessionId = AutopilotAnywhere.sessionId;


Call the add/update contact API call

Please note that:

  • email address is required, as we use it as a unique identifier for contacts in Autopilot; and
  • spaces in custom field names are represented as -- in the call; and
  • adding a custom field with the form’s name or ID is recommended to help you track of the source of the contacts.

You will use the POST Add/Update Contact API call to send this. You will send over any standard fields and custom fields you want to update as part of the body of the call. For example:

Method: POST

Endpoint: https://api2.autopilothq.com/v1/contact

Headers:

autopilotapikey = <Your API Key>

Content-Type = application/json

Body:

json

{ "contact": { "Email": "George@autopilot.com", "FirstName": "George", "LastName": "Giraffe", "_autopilot_session_id": "4ne73uvpS4ek54ryvOZtJ0PSBZNd8UW4moQn9lt9bqA",   "custom": {   "string--Form--Name": "Blog Subscriber",   "boolean--Newsletter": true     }   } }

If your call is accepted, you will receive a 200 response. A full list of other possible responses including errors and what they mean can be found in the API documentation.