# Call Okta API from Javascript

**URL:** <https://devforum.okta.com/t/call-okta-api-from-javascript/10334>\
**Category:** Questions\
**Created:** [July 30, 2020, 4:13pm UTC](https://devforum.okta.com/t/call-okta-api-from-javascript/10334 "2020-07-30T16:13:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![evitoria](https://avatars.discourse-cdn.com/v4/letter/e/9de053/32.png) [@evitoria](https://devforum.okta.com/u/evitoria)\
**Post date:** [July 30, 2020, 4:13pm UTC](https://devforum.okta.com/t/call-okta-api-from-javascript/10334/1 "2020-07-30T16:13:51Z")

</div>

Hello,

I’m trying to add a user to Okta using the Okta API. I can do that using Postman, using the collection Users (Okta API), in the Create User folder, the Post method Create Activated User With Password.

I need to replicate that from my Javascript code, so I have been trying to do that using the following Ajax call:

```
           var oktaParameters = {
                "profile": [{ "firstName": "FirstNameValue", "lastName": "LastNameValue", "email": "t1@testEmail.com", "login": "t1@testEmail.com" }],
                "credentials": [{ "password": { "value": "Password5!" }}]
            };
            $.ajax({
                url: 'https://domain.okta.com/api/v1/users?activate=true',
                type: 'POST',
                crossDomain: true,
                dataType: 'jsonp',
                headers: { "Authorization": "Bearer" + "APITOKENVALUE" },
                data: oktaParameters,
                error: function (request, xhr, status, error) {
                    console.log('Error! request.responseText = ', request.responseText);
                    console.log('Error! xhr = ', xhr);
                    console.log('Error! status = ', status);
                    console.log('Error! error = ', error);
                },
                success: function (data) {
                    console.log('Success!');
                }
            });

```

This Ajax call is not working. I get a parser error in the xhr result, but it doesn’t contain more details that can help me understand what is the problem.

Please share how to do this.

---

<div class="post-metadata">

**Author:** ![phi1ipp](https://avatars.discourse-cdn.com/v4/letter/p/85f322/32.png) [@phi1ipp](https://devforum.okta.com/u/phi1ipp)\
**Post date:** [August 1, 2020, 7:53pm UTC](https://devforum.okta.com/t/call-okta-api-from-javascript/10334/2 "2020-08-01T19:53:29Z")

</div>

Do you have a space in your Authorization header between Bearer and actual token? Also, don’t you need to JSON.stringify your data?

---

<div class="post-metadata">

**Author:** ![vijet](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/vijet/32/245_2.png) [@vijet](https://devforum.okta.com/u/vijet)\
**Post date:** [August 2, 2020, 5:17am UTC](https://devforum.okta.com/t/call-okta-api-from-javascript/10334/3 "2020-08-02T05:17:41Z")

</div>

Also, you shouldn’t be using the API Token if your javascript code is running in a front-end application.  
Anyone with access to your source would be able to view your token and make calls to your Okta org.  
You can use the “OAuth for Okta” feature to use an access token to call Okta APIs instead - [https://developer.okta.com/docs/guides/implement-oauth-for-okta/overview/](https://developer.okta.com/docs/guides/implement-oauth-for-okta/overview/)

---

<div class="post-metadata">

**Author:** ![phi1ipp](https://avatars.discourse-cdn.com/v4/letter/p/85f322/32.png) [@phi1ipp](https://devforum.okta.com/u/phi1ipp)\
**Post date:** [August 2, 2020, 1:12pm UTC](https://devforum.okta.com/t/call-okta-api-from-javascript/10334/4 "2020-08-02T13:12:43Z")

</div>

it might be backend nodeJS app

---

<div class="post-metadata">

**Author:** ![vijet](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/vijet/32/245_2.png) [@vijet](https://devforum.okta.com/u/vijet)\
**Post date:** [August 2, 2020, 7:40pm UTC](https://devforum.okta.com/t/call-okta-api-from-javascript/10334/5 "2020-08-02T19:40:22Z")

</div>

@evitoria - If it’s a backend nodejs app, then I’d recommend you use the [okta-sdk-nodejs](https://github.com/okta/okta-sdk-nodejs) instead of writing your own client functions.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/oktadev/original/1X/589c4c5f80de422d01f47cee7185a7fb8227835c.png) [@system](https://devforum.okta.com/u/system)\
**Post date:** [January 25, 2024, 4:52pm UTC](https://devforum.okta.com/t/call-okta-api-from-javascript/10334/6 "2024-01-25T16:52:36Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
