CMS Development

jillii
Participant

How to access API using hubl

SOLVE

Hello, I am trying to translate the following into hubl, so I can use it in a custom module. What this code does is it gets the current user's ID to extract data about them. Ultimately I am trying to figure out what Lists the current user is on.

  $user_id = $_COOKIE['hubspotutk'];
  $url = "https://api.hubapi.com/contacts/v1/contact/utk/" . $user_id . "/profile";

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  curl_setopt($curl, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer pat-na1-XXXXX-XXX-XXX-XXXX-XXXXXX',
    'Content-Type: application/json'
  ]);

  $response = curl_exec($curl);
  $data = json_decode($response, true);

 I've removed the autorization code for privacy. 

0 Upvotes
1 Accepted solution
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

How to access API using hubl

SOLVE

Hey @jillii 

 

So HUBL is a subset of Jinja, which is a templating language. By defenition templating languages do not have the ability to make http request. In your case you've got a few options:

  • if the user is cookied via form you can use the HUBL {{ contact }} variable to access basic information about the contact like email, name, and id.
  • you can use the ID from the above with JS to make an API call to a serverless function or external server to retrieve more data from the contact record

 

I would also nudge you to check out the available HUBL functions.

 

Hope this gets you moving!

Best

Kevin Cornett - Sr. Solutions Architect @ BridgeRev

View solution in original post

0 Upvotes
4 Replies 4
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

How to access API using hubl

SOLVE

Hey @jillii 

 

So HUBL is a subset of Jinja, which is a templating language. By defenition templating languages do not have the ability to make http request. In your case you've got a few options:

  • if the user is cookied via form you can use the HUBL {{ contact }} variable to access basic information about the contact like email, name, and id.
  • you can use the ID from the above with JS to make an API call to a serverless function or external server to retrieve more data from the contact record

 

I would also nudge you to check out the available HUBL functions.

 

Hope this gets you moving!

Best

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
jillii
Participant

How to access API using hubl

SOLVE

This is a great answer. My only follow up question is: wouldn't I need my private app key to access data with an API call - in which case using js would make it available on the frontend? That seems like a huge security risk.. or am I missing something?

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to access API using hubl

SOLVE

@jillii 

 

Yes you would, and this is where the serverless function/lambda comes into play. By running that request in a secure environment that has access to your auth you avoid security issues.

 

So your flow would be something like:

  1. page loads
  2. module JS makes request to serverless function
  3. serverless function makes authentiated request to hubspot and return the needed data
Kevin Cornett - Sr. Solutions Architect @ BridgeRev
jillii
Participant

How to access API using hubl

SOLVE

Aha, that makes sense. Could you please provide an example of that workflow?

0 Upvotes