# Create companies using XMLHttpRequest returning 400 Error

**URL:** <https://community.hubspot.com/t/create-companies-using-xmlhttprequest-returning-400-error/12044>\
**Category:** APIs & Integrations\
**Tags:** companies\
**Created:** [March 19, 2018, 6:03pm UTC](https://community.hubspot.com/t/create-companies-using-xmlhttprequest-returning-400-error/12044 "2018-03-19T18:03:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [March 19, 2018, 6:03pm UTC](https://community.hubspot.com/t/create-companies-using-xmlhttprequest-returning-400-error/12044/1 "2018-03-19T18:03:40Z")

</div>

I am attempting to create a company using the api, but I am getting a 400 error. Followed the api docs for expected params, so not sure why it is deemed a bad request.

var url=‘[https://api.hubapi.com/companies/v2/companies?hapikey=](https://api.hubapi.com/companies/v2/companies?hapikey=)’;  
var params = ‘{“properties”: [{“name”: “name”,“value”: “OK Company”}, {“name”: “description”,“value”: “Ok company” }]}’;  
var xhr = new XMLHttpRequest();  
var json = JSON.stringify(params);  
alert(json);

xhr.open(“POST”, url, true);  
xhr.setRequestHeader(‘Content-type’,‘application/json’);  
xhr.onload = function () {  
if (xhr.readyState == 4 && xhr.status == “200”) {  
alert(“SUCCESS”);  
} else {  
alert(json + “FAILED”);  
}  
}  
xhr.send(json);

---

<div class="post-metadata">

**Author:** ![Derek\_Gervais](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/derek_gervais/32/17429_2.png) [@Derek\_Gervais](https://community.hubspot.com/u/Derek_Gervais)\
**Post date:** [March 20, 2018, 3:25pm UTC](https://community.hubspot.com/t/create-companies-using-xmlhttprequest-returning-400-error/12044/2 "2018-03-20T15:25:04Z")

</div>

Hi [@clevy](https://community.hubspot.com/u/clevy),

Are you trying to make this request client-side? The HubSpot APIs don’t support support cross-origin (CORS) AJAX requests. Making the request client-side using JavaScript would expose any authentication you’re using for the request. In order to use JavaScript/AJAX, you’ll need to make the request (excluding any authentication) to an external server that could then add the needed authentication and make requests to HubSpot’s APIs server-side.

![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/9/0/9030b41089ba24b4a0f4bb107832addd890c270c.png) [developers.hubspot.com](https://developers.hubspot.com/docs/faq/do-hubspot-apis-support-ajax-request)

### [Do HubSpot APIs support CORS / AJAX requests?](https://developers.hubspot.com/docs/faq/do-hubspot-apis-support-ajax-request)

At this time, HubSpot APIs do not support support cross-origin (CORS) AJAX requests. In order to use AJAX, you would need to make the request to an external proxy server that could then make requests to HubSpot’s APIs.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [March 20, 2018, 6:31pm UTC](https://community.hubspot.com/t/create-companies-using-xmlhttprequest-returning-400-error/12044/3 "2018-03-20T18:31:32Z")

</div>

Thanks for responding [@Derek\_Gervais](https://community.hubspot.com/u/derek_gervais). I am aware of the CORS limitation, and I am working around it for testing, also using the test portal, when the application is live, I will make the request on an external server.

I solved my issue in case anyone else runs into it. The script was escaping my param arguments and which was causing a malformed json.

Changed stringify to no longer escape my braces.
