I'm getting 401 error with new Hubspot API for publish a hubdb table

With the code bellow i receive this message

“Request failed with status code 401” that apparently is due to authentication issues, but the access token is right and the scope too for hubdb connections, i don’t know why is the error, i’m using it in a serverless function inside my hubspot account.

const axios = require(‘axios’);
const accessToken = “My access token recently created”;

exports.main = (context, sendResponse) => {

axios.post(“https://api.hubapi.com/cms/v3/hubdb/tables/“my table”/draft/publish”,
{
headers: {
‘Content-Type’ : ‘application/json’,
‘Authorization’ : `Bearer ${accessToken}`
}
}
)
.then(function (response) {
sendResponse({body: JSON.stringify(response), statusCode: 200})
})
.catch(function (error) {
sendResponse({body: JSON.stringify(error), statusCode: 500})
})
}

I tried using the code hubspot recommeded from Accounts Dashboard | HubSpot but the path seems to be bad, because it brings me the error “TypeError: Cannot read property ‘hubdb’ of undefined” when i use “hs logs”, so i found this post that apperars to be the same error with other path but i tried with others and i can’t found the right one.

@ESalas7 Any luck with this? I am stuck in the same position, caught out by the documentation at Accounts Dashboard | HubSpot which refers to a bad path.

Hi JOConnell2, yes i found the solution changing the code like this.

const axios = require(‘axios’);
const accessToken = “My access token recently created”;

exports.main = (context, sendResponse) => {

axios.post(“https://api.hubapi.com/cms/v3/hubdb/tables/“my table”/draft/publish”,
{
headers: {
‘Content-Type’ : ‘application/json’,
‘Authorization’ : `Bearer ${accessToken}`
}
}
)
.then(function (response) {
sendResponse({body: JSON.stringify(response.data), statusCode: 200})
})
.catch(function (error) {
sendResponse({body: error, statusCode: 500})
})
}

The only thing i changed was adding de “response.data” into the send response function and magically everything rans perfectly, i hope this works for you too.

@ESalas7 Thanks for that! This worked perfectly. I didn’t realise you already replied with this above. The order of the replies and the indentation was a little confusing. Thanks for your help.

i am facing the same issue,but with tickets api , any update on it ?

Hello VSkyhype, i found the solution changing only one thing in the first code i presented.

const axios = require(‘axios’);
const accessToken = “My access token recently created”;

exports.main = (context, sendResponse) => {

axios.post(“https://api.hubapi.com/cms/v3/hubdb/tables/“my table”/draft/publish”,
{
headers: {
‘Content-Type’ : ‘application/json’,
‘Authorization’ : `Bearer ${accessToken}`
}
}
)
.then(function (response) {
sendResponse({body: JSON.stringify(response.data), statusCode: 200})
})
.catch(function (error) {
sendResponse({body: error, statusCode: 500})
})
}

The only thing i changed was adding de “response.data” into the send response function and magically everything rans perfectly :), i hope this works for you too.

still getting the same error(401) but working perfectly when using postman, I am making axios post request from react app fromtend as:

   axios.defaults.headers = {

    "Accept": "application/json",

    "Content-Type": "application/json",

    Authorization: \`Bearer ${token}\`

  };

  axios.post('https://api.hubapi.com/crm-objects/v1/objects/tickets',data)

  .then((response)=>{

console.log(response.data)

  })