APIs & Integrations

SMCNULTY
Participant

Trying my first API call from VS Code

SOLVE

Hello,

   I'm just starting out trying to make my first API call to HS.  I'm a complete NOOB.  I think what I'm doing might be getting phased out but I wanted to start here and work my way up.     I am using VS Code and have a handful of extensions installed.   (REST Client / Thunder ) I have node.JS installed.  Outside of Hubspot, I've been able to successfully run a "GET" call to a test website and it returned JSON data.  

 

I'm now looking at some archived instructions per the snippet below.  I've created a private app (access to literally all options) and have my token ready.

 

I've tried pasting the node.JS code (below) into VScode and also tried a GET command on the example http address shown in the snippet (with my token pasted in) but neither is working.

 

I tried this with my own email address (which is a contact in HS) and I'm getting a message that the Hapikey does not exist.  

 

SMCNULTY_1-1679071909969.png

 

 

SMCNULTY_0-1679071847393.png

 

 

SMCNULTY_0-1679071088355.png

 

 

UPDATE:

I bounced completely over to POSTMAN and ran a GET as shown below:

 

1.) My private app token is stored as a secret Global variable

2.) Parameters Key Properties value is email

3.) Authorization is 'Bearer Token'

 

and getting the error shown below. 

SMCNULTY_1-1679077743391.pngSMCNULTY_2-1679077766665.pngSMCNULTY_3-1679077776528.png

 

 

 

 

SMCNULTY_0-1679077164514.png

 

0 Upvotes
1 Accepted solution
Jaycee_Lewis
Solution
Community Manager
Community Manager

Trying my first API call from VS Code

SOLVE

Hi, @SMCNULTY 👋 I think I can help out here.

 

Let's start with this endpoint — Get a contact by email address. Looking at your screenshot, it looks like you are trying to include your bearer token in the request URL. This won't work. Private Apps, and OAuth, require you to send the Authorization in the header. Meaning, instead of including it in the URL like the example:

https://api.hubapi.com/contacts/v1/contact/email/testingapis@hubspot.com/profile?hapikey=demo

you'll need to include it in your header — bearer token in the Authorization HTTP header. So, an example using Node might look like this example Postman provided:

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://api.hubapi.com/contacts/v1/contact/email/bob@bobsburgers.com/profile',
  'headers': {
    'Authorization': 'Bearer pat-na1-NOPE'
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

 

For your second question, we can also use Postman to help us out. What tricky about this one is getting the request URL set up correctly, so you can search by email address instead of ID.

 

It seems like the way you're trying to include the bearer token in Postman is different from how I do it. I'll add screenshots of what my set-up looks like below.

 

Here's an example:

  • Request: 
    GET https://api.hubapi.com/crm/v3/objects/contacts/tina%40bobsburgers.com?archived=false&idProperty=email​
  • Response: 
    {
        "id": "1001",
        "properties": {
            "createdate": "2022-11-03T21:42:31.255Z",
            "email": "tina@bobsburgers.com",
            "firstname": "Tina",
            "hs_object_id": "1001",
            "lastmodifieddate": "2023-02-01T17:33:35.259Z",
            "lastname": "Belcher"
        },
        "createdAt": "2022-11-03T21:42:31.255Z",
        "updatedAt": "2023-02-01T17:33:35.259Z",
        "archived": false
    }​

CleanShot 2023-03-17 at 15.39.38.png

CleanShot 2023-03-17 at 15.41.26.png

 

 

If this isn't quite what you are after, please let us know, and we will get you pointed in the right direction.

 

Best,

Jaycee 

 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

View solution in original post

0 Upvotes
4 Replies 4
SMCNULTY
Participant

Trying my first API call from VS Code

SOLVE

Jaycee,

 Thank you very much for the response.  I've actually had great luck today in Postman. (after weeks of struggling)  I'm going to hold off on VS Code for a while and just get everything working that I want to use in PM.  I'm now able to use PM and successfully used Gets for contact info/ all owners / and to get ticket details.  See image below.   Currently stuck on creating a ticket though so if you have a comment on this "The request body may not be null" that's where I'm at.   

In this case "hubspot_owner_ID" 104607097 should be me.

 

SMCNULTY_0-1679090381759.png

SMCNULTY_1-1679090552042.png

 

0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Trying my first API call from VS Code

SOLVE

Hey hey, @SMCNULTY! Let's take a look at my request, and maybe you can spot what's different on your end. Additionally, can you grab a screenshot of how your request body is set in Postman, please?

 

In the body tab, I set mine to “raw” and “JSON”. Like this:

CleanShot 2023-03-17 at 16.27.23.png

Here's my request URL, JSON body, and the response using the Tickets API and an example request body from the page — CRM API | Tickets.

 

POST {{API URL}}/crm/v3/objects/tickets
{
    "properties": {
        "hs_pipeline": "19625033",
        "hs_pipeline_stage": "47925262",
        "hs_ticket_priority": "HIGH",
        "subject": "Cats and ninjas",
        "hubspot_owner_id": "198670650"
    },
    "associations": [
        {
            "to": {
                "id": 901
            },
            "types": [
                {
                    "associationCategory": "HUBSPOT_DEFINED",
                    "associationTypeId": 16
                }
            ]
        }
    ]
}
{
    "id": "1503346867",
    "properties": {
        "createdate": "2023-03-17T22:27:14.029Z",
        "hs_all_accessible_team_ids": "12073943",
        "hs_all_owner_ids": "198670650",
        "hs_all_team_ids": "12073943",
        "hs_helpdesk_sort_timestamp": "2023-03-17T22:27:14.029Z",
        "hs_lastmodifieddate": "2023-03-17T22:27:14.029Z",
        "hs_object_id": "1503346867",
        "hs_pipeline": "19625033",
        "hs_pipeline_stage": "47925262",
        "hs_ticket_id": "1503346867",
        "hs_ticket_priority": "HIGH",
        "hs_user_ids_of_all_owners": "10233975",
        "hubspot_owner_assigneddate": "2023-03-17T22:27:14.029Z",
        "hubspot_owner_id": "198670650",
        "hubspot_team_id": "12073943",
        "subject": "Cats and ninjas"
    },
    "createdAt": "2023-03-17T22:27:14.029Z",
    "updatedAt": "2023-03-17T22:27:14.029Z",
    "archived": false
}

 

 

Best,

Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
SMCNULTY
Participant

Trying my first API call from VS Code

SOLVE

Interestingly enough I have nothing (well a '1') in my POST Body.  I checked my "working" GET body's and those also all show '1'.  The Get's work but the Post does not. 

I'm not sure why nothing is showing up in the Body>raw>JSON field.   

 

>>EDIT>> I put the JSON code into the RAW>BODY of my create ticket POST and I was able to create a ticket. That's good. Just not sure why when I put the fields in under parameters tab it didn't do the same thing.  This may be more of a Postman forum question than HS so ok to close this out if needed. 

 

This is my attempt at a POST and it does not work

SMCNULTY_0-1679324157306.png

SMCNULTY_1-1679324239606.png

 

This is a GET which works fine:

SMCNULTY_3-1679324322282.pngSMCNULTY_4-1679324357687.png

 

 

 

0 Upvotes
Jaycee_Lewis
Solution
Community Manager
Community Manager

Trying my first API call from VS Code

SOLVE

Hi, @SMCNULTY 👋 I think I can help out here.

 

Let's start with this endpoint — Get a contact by email address. Looking at your screenshot, it looks like you are trying to include your bearer token in the request URL. This won't work. Private Apps, and OAuth, require you to send the Authorization in the header. Meaning, instead of including it in the URL like the example:

https://api.hubapi.com/contacts/v1/contact/email/testingapis@hubspot.com/profile?hapikey=demo

you'll need to include it in your header — bearer token in the Authorization HTTP header. So, an example using Node might look like this example Postman provided:

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://api.hubapi.com/contacts/v1/contact/email/bob@bobsburgers.com/profile',
  'headers': {
    'Authorization': 'Bearer pat-na1-NOPE'
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

 

For your second question, we can also use Postman to help us out. What tricky about this one is getting the request URL set up correctly, so you can search by email address instead of ID.

 

It seems like the way you're trying to include the bearer token in Postman is different from how I do it. I'll add screenshots of what my set-up looks like below.

 

Here's an example:

  • Request: 
    GET https://api.hubapi.com/crm/v3/objects/contacts/tina%40bobsburgers.com?archived=false&idProperty=email​
  • Response: 
    {
        "id": "1001",
        "properties": {
            "createdate": "2022-11-03T21:42:31.255Z",
            "email": "tina@bobsburgers.com",
            "firstname": "Tina",
            "hs_object_id": "1001",
            "lastmodifieddate": "2023-02-01T17:33:35.259Z",
            "lastname": "Belcher"
        },
        "createdAt": "2022-11-03T21:42:31.255Z",
        "updatedAt": "2023-02-01T17:33:35.259Z",
        "archived": false
    }​

CleanShot 2023-03-17 at 15.39.38.png

CleanShot 2023-03-17 at 15.41.26.png

 

 

If this isn't quite what you are after, please let us know, and we will get you pointed in the right direction.

 

Best,

Jaycee 

 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes