Using Components: Rest API Destination


In this short tutorial, we'll show how to create or update contacts in Hubspot as an example of writing data to any REST API destination.

Writing data to REST APIs essentially works in the same manner as reading data from REST APIs - using simple HTTP requests. HTTP requests are comprised of 4 parts:

  • Method - the most common are GET (used to read data) and POST (to write data).
  • URL - specifies the resource to read/write
  • Headers - specify additional information to the server (e.g. authentication keys, request body format)
  • Body - usually contains the data to send to the server in JSON or query string format)

In this tutorial, we'll use the Curl function to make HTTP requests to create or update contacts in Hubspot.

According to Hubspot docs, for each contact, we have to generate a URL containing the contact's email - used as the contact identifier - and a JSON string in the following form to submit in the request body:

{
  "properties": [
    {
      "property": "property name",
      "value": "property value"
    },
    ...
    ]
}

In our package, we first read data from a contacts table in a source database.


In the second component, we generate the URL and the JSON string.

prepare_request

The URL contains the email address and the Hubspot API key (which we store in a package variable - $hapikey).

SPRINTF('https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/%s/?hapikey=%s', URLEncode(email), '$hapikey')

The JSON string is generated using Xplenty's functions to build nested arrays and objects, TOBAG and TOMAP, correspondingly, and ToJson that returns a JSON string. The JSON structure specified by Hubspot is relatively complex (an array of objects within an object) where most APIS usually work well with a simple JSON object.

ToJson(TOMAP('properties',TOBAG(TOMAP('property','firstname','value',firstname),TOMAP('property','lastname','value',lastname),TOMAP('property','website','value',website),TOMAP('property','company','value',company),TOMAP('property','phone','value',phone),TOMAP('property','address','value',address),TOMAP('property','city','value',city),TOMAP('property','state','value',state),TOMAP('property','zip','value',zip))))

In the third component, the request is made, per record coming in from the source table, and a response is returned.

make_request

The response contains a status code, headers and a body. The status code determines whether the request was successful or not (200 is returned on success. Read more here).

(int)Curl(url,'POST','',body)#'status'

Finally, the contact email and the response code are stored into a database table. This helps us monitor the process.

In the same manner, you can write data to any REST API endpoint - simply go through the API docs, build the URL and request body using the source data and check the response to make sure the returned response indicates success.



Creating packages

  1. Creating a new package in New Xplenty
  2. Creating a workflow
  3. Working in the new package designer
  4. Validating a package
  5. Using components: Amazon Redshift Source
  6. Using components: Bing Ads Source
  7. Using components: Database Source
  8. Using components: Facebook Ads Insights Source
  9. Using components: File Storage Source
  10. Using components: Google Adwords source
  11. Using components: Google Analytics Source
  12. Using components: Google BigQuery Source
  13. Using components: Google Cloud Spanner Source
  14. Using components: MongoDB Source
  15. Using components: NetSuite Source
  16. Using components: Salesforce source
  17. Using components: Rest API Source
  18. Using components: Aggregate Transformation
  19. Using components: Assert Transformation
  20. Using components: Clone transformation
  21. Using components: Cross Join Transformation
  22. Using components: Distinct Transformation
  23. Using components: Filter Transformation
  24. Using components: Join Transformation
  25. Using components: Limit Transformation
  26. Using components: Rank Transformation
  27. Using components: Select Transformation
  28. Using components: Sort Transformation
  29. Using components: Union Transformation
  30. Using components: Window Transformation
  31. Using components: Sample Transformation
  32. Using components: Cube transformation
  33. Using components: Amazon Redshift Destination
  34. Using components: Database Destination
  35. Using components: File Storage Destination
  36. Using components: Google BigQuery Destination
  37. Using components: Google Spanner Destination
  38. Using components: MongoDB Destination
  39. Using components: Salesforce Destination
  40. Using components: Snowflake Destination (beta)
  41. Using Components: Rest API Destination
  42. Using and setting variables in your packages
  43. System and pre-defined variables
  44. Using pattern-matching in source component paths
  45. Using ISO 8601 string functions
  46. Using Expressions in Xplenty
  47. Xplenty Functions

Feedback and Knowledge Base