Part 2 - Connecting FileMaker to Kartra
In Part 1 of this series, we took a look at the overall project of connecting FileMaker to Kartra and WordPress. In Part 2, we'll get into the details of connecting FileMaker to Kartra's inbound API. Specifically, the ability to import updated information, such as sales data, lead (customer) information, list information, and subscription information into FileMaker, as well as updating or adding information to Kartra from FileMaker. In Part 3, we'll connect FileMaker to WordPress using a custom JSON API program.
As a quick review, remember a Lead, in Kartra, is information about a contact, and working with Lead information and status is the focal point of the API. Using the API, a Lead can be created, edited, searched, subscribed to lists and tags, unsubscribed, and more. Here is a partial list of actions we can complete using the API:
- Creating, Editing or Searching a Lead
- Retrieving data from a lead
- Subscribe or unsubscribe a lead to a list
- Assign or un-assign a tag to a lead
- Remove or add points to a lead
- Subscribe or unsubscribe a lead from a sequence
- Refund or cancel a Kartra transaction
- Search transactions or subscriptions
- Get transactions or subscriptions from a lead
Let's get started.
Step One: Create a Kartra App
Creating a Kartra App is easy: Navigate to My Integrations>Developers>API to get started. You'll need to be a member of Kartra (there is a fee - it's not free) to complete this step, and your application will be subject to review before approval. And, since
Kartra also provides a Test Mode, allowing you to get everything set up before interacting with your Kartra customers. Once your App is approved, you'll get an App ID and API keys, which you will use every time you connect using the API.
Making the Connection
Finally, it's time to actually interact with the Kartra API. We'll do this in three parts:
- Set up a variable ($URL) with the endpoint URL
- Set up a variable ($CREDS) with the App ID and the API Keys
- Build the rest of the command using the API and fields from FileMaker using cURL.
For this example, we'll be adding a lead to Kartra from a FileMaker database - in other words, we will add a new user from FileMaker to Kartra. To do this, Kartra requires two pieces of information:
- A cmd (command) – in this case the command is "create_lead"
- A unique (to Kartra) email address
We can also add all the details about the lead, including first and last name, a phone number, address, company info, etc. For this demo, we'll just add the first and last name.
The endpoint (the location we will send our command) for Kartra is "https://app.kartra.com/api". Set a variable with that string as the contents:
$URL = "https://app.kartra.com/api"
The API information is next:
$CREDS = "--data app_id = BnF4dyseiO & api_key = Yn4eSGdT & api_password = 7mEKWdeYONNF"
Finally, we add the information and the command needed to create a Lead with the Lead's email, first name, and last name.
& lead[email] = [email protected] & lead[first_name] = Don & lead[last_name] = Clark & actions[cmd] = create_lead"
Using FileMaker's Insert from URL command, we set the results to be stored in a variable called $$Results. Sending the above command returns a JSON data packet:
Use the JSONFormatElements command to see the results in a more user friendly way. We'll set a new variable to hold that data and to work with it going forward:
Set Variable[($$JSON; Value: JSON(FormatElements($$Results)].
It's not necessary to format the elements to parse the data, but it helps when you are trying to learn a new system. Here is the more friendly view of our new variable, $$JSON:
Next, use the JSONListKeys to get a list of keys. In this example, there is only one key: . (Don't forget that indexed data starts counting at zero, not one). Here is the command and the results:
JSONListKeys($$Results) = 0
Next, use JSONListValues to isolate the values of the key:
JSONListValues ( $$Results ; "actions.create_lead" )
The results are:
We can easily test for the success of the query now:
if(JSONGetElement ( $$results ; "actions.create_lead.status" ) ≠ "Success"
If the above is true, halt the script with an error code and add some logic to try again (or whatever makes sense for your solution). Otherwise, get the lead ID from the JSON packet and store it in a FileMaker variable or field.
Set Variable($ID; Value("JSONGetElement ( $$Results; "actions.create_lead.lead_details.id" )).
In reality, a developer does not need to see all of this information...you need only to understand what is happening behind the scenes and how to process the packets to test for success and handle the resulting data for both success and failure.
Using cURL and Insert from URL, we successfully uploaded information from FileMaker to Kartra, and stored return information from Kartra in FileMaker. Using the same format, we can now successfully exchange information with Kartra: transaction history, lead management and more.