wedman
wedman8mo ago

Making Post Request To HubSpot Api

Hi there I'm trying to make a post request to the HubSpot Api. However the request results in a "HubSpot API responded with status: 401" ´401 Unauthorized is returned when the authentication provided is invalid.´ https://developers.hubspot.com/docs/api/crm/contacts Selected scopes on the HubSpot app: crm.objects.companies.read crm.objects.contacts.read crm.objects.contacts.write crm.objects.companies.write
const body = {
"properties": {
"email": data.email,
"phone": "(877) 929-0687",
"company": "Biglytics",
"website": "biglytics.net",
"lastname": data.lastname,
"firstname": data.firstname
}
};

// Make the POST request to HubSpot
const response = await fetch('https://api.hubapi.com/crm/v3/objects/contacts', {
method: 'POST',
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${HUBSPOT_ACCESS_TOKEN}`
},
body: JSON.stringify(body)
});

if (response.ok) {
const result = await response.json();
console.log(result);
} else {
throw new Error(`HubSpot API responded with status: ${response.status}`);
}
const body = {
"properties": {
"email": data.email,
"phone": "(877) 929-0687",
"company": "Biglytics",
"website": "biglytics.net",
"lastname": data.lastname,
"firstname": data.firstname
}
};

// Make the POST request to HubSpot
const response = await fetch('https://api.hubapi.com/crm/v3/objects/contacts', {
method: 'POST',
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${HUBSPOT_ACCESS_TOKEN}`
},
body: JSON.stringify(body)
});

if (response.ok) {
const result = await response.json();
console.log(result);
} else {
throw new Error(`HubSpot API responded with status: ${response.status}`);
}
All help is super appreciative! Thank you in advance!
CRM API | Contacts
Contact records store information about individuals. The contacts endpoints allow you to manage this data and sync it between HubSpot and other systems.
2 Replies
cknight
cknight8mo ago
Looks like your HUBSPOT_ACCESS_TOKEN is not valid. If you console.log it before your fetch, is the value what you expect? Also, if it's an environment variable, are you referencing it correctly? E.g. Deno.env.get(HUBSPOT_ACCESS_TOKEN) perhaps?
wedman
wedman8mo ago
I did a log on it now. it is null hehe thank you!