Error Creating Contact

I am trying to create a contact in Hubspot through an API call from another application.

The contact is created successfully, but I an encounter an error when trying to retrieve the Id value of the newly created contact.

The first line of code executes successfully and the contact is created:

const response = await fetch(endpointUrl, config);

The next line of code results in a type error:
“response.json is not a function”

const responseBody = await response.json();

I’m also getting this error while creating a contact via API. Tried everything but still stuck. Did anyone find a fix for this?

Hi @KShiban

Hey, I just tried to create the contact using API, but I was able to create and the contact ID in the response body as well.
Here is the curl -

curl --request POST \
 --url https://api.hubapi.com/crm/v3/objects/contacts \
 --header 'authorization: Bearer ' \
 --header 'content-type: application/json' \
 --data '{
 "properties": {
	"email": "v.josh@gn.com",
	"lastname": "Josh",
	"firstname": "vayu"
 }
}'

link - Accounts Dashboard | HubSpot

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.

Thanks!

That’s great, but I have to be able to do with JavaScript, not cURL.

Hey, @KShiban :waving_hand:

To help us debug this, can you share a bit more information?

  1. Could you share the more complete code block for the function making this API call?
  2. What HTTP library are you using (node-fetch, axios, etc.)?
  3. Could you add console.log(response) on the line right after your fetch call and share the output here?

I hope the console.log will tell us exactly what kind of object you’re working with and how to get the data from it.

Talk soon! — Jaycee

Here is the complete function. It creates a new contact in HubSpot from VoiceFlow. The input variables are:
Email
LastName
FirstName
roi_url
hubspotApiToken
export default async function main(args) {
// Validate input variables from args - find the “validateInputs” function below
const invalidFieldTrace = validateInputs(args);
if (invalidFieldTrace) return invalidFieldTrace;
const {
Email,
FirstName,
LastName,
roiUrl,
hubspotApiToken,
} = args.inputVars;
const payload = {
properties: {
email: Email,
firstname: FirstName,
lastname: LastName,
roi_url: roiUrl,
}
};
const endpointUrl = ‘https://api.hubapi.com/crm/v3/objects/contacts’;
const config = {
method: ‘POST’,
headers: {
‘Authorization’: `Bearer ${hubspotApiToken}`,
‘Content-Type’: ‘application/json’,
},
body: JSON.stringify(payload)
};
try {
const response = await fetch(endpointUrl, config);
if (!response.ok) {
throw new Error(`HTTP status code ${response.status}`);
}
const responseBody = await response.json();
if (!responseBody || typeof responseBody !== ‘object’) {
throw new Error(“Invalid or missing response body”);
}
const contactId = responseBody.id;
return {
outputVars: {
contactId,
},
next: {
path: ‘success’
},
trace: [
{
type: “debug”,
payload: {
message: ‘HubSpot API call successful’
}
},
{
type: “debug”,
payload: {
message: `Contact created - ID: ${contactId}`
}
},
]
};
} catch (error) {
return {
next: {
path: ‘error’
},
trace: [{
type: “debug”,
payload: {
message: `HubSpot API call error: ${error.message}`
}
}]
};
}
function validateInputs(args) {
const {
Email,
FirstName,
LastName,
hubspotApiToken,
} = args.inputVars;
if (!FirstName) {
return {
next: { path: ‘error’ },
trace: [{ type: “debug”, payload: { message: “Missing required input variable: FirstName” } }]
};
}
if (!Email) {
return {
next: { path: ‘error’ },
trace: [{ type: “debug”, payload: { message: “Missing required input variable: Email” } }]
};
}
if (!LastName) {
return {
next: { path: ‘error’ },
trace: [{ type: “debug”, payload: { message: “Missing required input variable: LastName” } }]
};
}
if (!hubspotApiToken) {
return {
next: { path: ‘error’ },
trace: [{ type: “debug”, payload: { message: “Missing required input variable: hubspotApiToken” } }]
};
}
return null;
}
}

Hey, @KShiban :waving_hand: It sounds like your 3rd party system is having an issue with `fetch`. I’d try updating your code to check if response.json is a function. If it is, it proceeds as normal. If not, it assumes the response is the data itself. — Jaycee