# Bulk User Imports, Node js example does not work

**URL:** <https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482>\
**Category:** Get Help\
**Created:** [May 10, 2021, 1:27am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482 "2021-05-10T01:27:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam.butterfield](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adam.butterfield/32/12951_2.png) [@adam.butterfield](https://community.auth0.com/u/adam.butterfield)\
**Post date:** [May 10, 2021, 1:27am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/1 "2021-05-10T01:27:06Z")

</div>

Hi,

I am following this guide right here, but the Node example does not seem to work: [Bulk User Imports](https://auth0.com/docs/users/bulk-user-imports#request-bulk-user-import)

The exact code I’m trying to run (with sensitive bits omitted):

```
var axios = require("axios").default;
const url = "https://XXX/api/v2/jobs/users-imports";
const token = "XXX";
const connectionId = "XXX";
const filename = "users.json"

var options = {
  method: "POST",
  url,
  headers: {
    authorization: `Bearer ${token}`,
    "content-type": "multipart/form-data; boundary=---011000010111000001101001",
  },
  data: `-----011000010111000001101001\r\nContent-Disposition: form-data; name="users"; filename="${filename}"\r\nContent-Type: text/json\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name="connection_id"\r\n\r\n${connectionId}\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name="external_id"\r\n\r\n\r\n-----011000010111000001101001--\r\n`,
};

axios
  .request(options)
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.error(error);
  });

```

The error I’m currently getting:

```
data: {
  statusCode: 400,
  error: 'Bad Request',
  message: 'Users file must not be empty',
  errorCode: 'operation_not_supported'
}

```

Anyone can help me with this? (And maybe the docs could be updated with a working example?)

p.s. I have already tried to modify in different ways (following posts on here and stack overflow), and nothing has worked, always getting some kind of error…

---

<div class="post-metadata">

**Author:** ![nissandookeran](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nissandookeran/32/27153_2.png) [@nissandookeran](https://community.auth0.com/u/nissandookeran)\
**Post date:** [May 10, 2021, 6:07am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/2 "2021-05-10T06:07:36Z")

</div>

Have you ensured the path of the `users.json` file you are using here is in the path the NodeJS script is looking for it in, and that the users.json file is not an empty file? There’s a basic example of the `users.json` file at [Bulk User Import Database Schema and Examples](https://auth0.com/docs/users/bulk-user-import-database-schema-and-examples#basic-example) you can use for testing, with more complex examples throughout that same page. Hope this helps.

---

<div class="post-metadata">

**Author:** ![adam.butterfield](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adam.butterfield/32/12951_2.png) [@adam.butterfield](https://community.auth0.com/u/adam.butterfield)\
**Post date:** [May 12, 2021, 4:22am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/3 "2021-05-12T04:22:39Z")

</div>

The file with the upload code is in the same directory as users.json. And users.json is not an empty file…

---

<div class="post-metadata">

**Author:** ![nissandookeran](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nissandookeran/32/27153_2.png) [@nissandookeran](https://community.auth0.com/u/nissandookeran)\
**Post date:** [May 14, 2021, 7:55am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/4 "2021-05-14T07:55:49Z")

</div>

Does it give the same behavior/error message with the sample file at the link shared above, specifically copying and pasting [Bulk User Import Database Schema and Examples](https://auth0.com/docs/users/bulk-user-import-database-schema-and-examples#basic-example) sample data into a users.json file and using this as the source of users for the upload script?

- If it does give the same error, then you may want to double check the path you are using for referencing the file, as it would most likely be that it simply isn’t seeing the file.
- If it does not give the same error and uploads correctly, can you try copying the data from the original non-working users.json file and pasting and overwriting the data in the file that works?

This will then reveal if it is a file data error if this doesn’t work, or an actual saved file format error if this does work, as is sometimes (depending on your OS) where a file editor may use a file encoding that doesn’t quite work as expected with an HTTP POST.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![adam.butterfield](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adam.butterfield/32/12951_2.png) [@adam.butterfield](https://community.auth0.com/u/adam.butterfield)\
**Post date:** [May 14, 2021, 8:04am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/5 "2021-05-14T08:04:04Z")

</div>

How else would I refer to the file? Shouldn’t it be clear or not my the code above if it would see the file or not? (Which is basically the same as the Auth0 example, I just set it to a variable instead of having it inline).

---

<div class="post-metadata">

**Author:** ![ldibello](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ldibello/32/22011_2.png) [@ldibello](https://community.auth0.com/u/ldibello)\
**Post date:** [May 21, 2021, 8:39pm UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/6 "2021-05-21T20:39:44Z")

</div>

Hi, I got the same error, using python.  
I use full path to json file and relative path and both the same error, if I exec from curl works fine. so the file is well json formed.  
Any Help?

---

<div class="post-metadata">

**Author:** ![jdrake](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jdrake/32/8318_2.png) [@jdrake](https://community.auth0.com/u/jdrake)\
**Post date:** [August 4, 2021, 9:46pm UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/7 "2021-08-04T21:46:44Z")

</div>

I’m having the same issue. Have tried relative and absolute paths. JSON file is well formed.

---

<div class="post-metadata">

**Author:** ![thameera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thameera/32/13430_2.png) [@thameera](https://community.auth0.com/u/thameera)\
**Post date:** [August 5, 2021, 8:20am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/8 "2021-08-05T08:20:43Z")

</div>

Can you try the following script? You’ll need to `npm i form-data`.

```javascript
const axios = require("axios")
const FormData = require('form-data')
const fs = require('fs')

const FILENAME = 'example.json'
const AUTH0_DOMAIN = 'example.auth0.com'
const TOKEN = 'REPLACE_WITH_TOKEN'

const run = async () => {
  const formData = new FormData()
  formData.append("connection_id", "con_abcdef1234") // Replace with correct connection ID
  formData.append("users", fs.createReadStream(FILENAME))

  try {
    const res = await axios.post(`https://${AUTH0_DOMAIN}/api/v2/jobs/users-imports`, formData, {
      headers: Object.assign({}, formData.getHeaders(), {
        authorization: `Bearer ${TOKEN}`
      })
    })
    console.log(res.data)
  }
  catch (err) {
    console.log(err)
  }
}

run()

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [August 5, 2021, 9:15am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/9 "2021-08-05T09:15:35Z")

</div>

Thanks for helping on this one Thameera!

---

<div class="post-metadata">

**Author:** ![ciem](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ciem/32/13332_2.png) [@ciem](https://community.auth0.com/u/ciem)\
**Post date:** [August 26, 2021, 2:04am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/10 "2021-08-26T02:04:58Z")

</div>

Hi, I’m seeing this issue in python, and the input file is not empty. Is the python snippet provided in the guide out of date? Guide: [Bulk User Imports](https://auth0.com/docs/users/bulk-user-imports).

---

<div class="post-metadata">

**Author:** ![ciem](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ciem/32/13332_2.png) [@ciem](https://community.auth0.com/u/ciem)\
**Post date:** [August 27, 2021, 5:00pm UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/11 "2021-08-27T17:00:28Z")

</div>

Hi there could someone help with my question? Thanks! I’d also like to add that I ran the curl command with my own parameters, and it worked fine. It seems to be the python snippet.

---

<div class="post-metadata">

**Author:** ![fdes](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/fdes/32/20969_2.png) [@fdes](https://community.auth0.com/u/fdes)\
**Post date:** [September 7, 2021, 9:51pm UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/12 "2021-09-07T21:51:28Z")

</div>

Hi, same applies to the C# snippet. It doesn’t work the way it seems to be setup. Saw some examples online that use RestRequest and they added the file with ‘request.AddFile()’ method so maybe something is missing in those code examples for User imports?

---

<div class="post-metadata">

**Author:** ![thameera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thameera/32/13430_2.png) [@thameera](https://community.auth0.com/u/thameera)\
**Post date:** [September 8, 2021, 9:24am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/13 "2021-09-08T09:24:35Z")

</div>

@ciem The following code seems to work fine, but my Python is rusty so don’t consider this production-ready code:

```python
import requests

TOKEN='REPLACE_WITH_TOKEN'

url = 'https://TENANT.auth0.com/api/v2/jobs/users-imports'
headers = { 'Authorization': 'Bearer ' + TOKEN }

payload = ( ('users', open('tmp.json', 'rb')) , ('connection_id', (None, 'con_abcdef1234')) )

r = requests.post(url, headers=headers, files=payload)
print(r.text) # You may need to parse the JSON in r.text

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [September 24, 2021, 11:24am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/14 "2021-09-24T11:24:10Z")

</div>

Thanks for helping on this one Thameera!

---

<div class="post-metadata">

**Author:** ![dhaich](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dhaich/32/36089_2.png) [@dhaich](https://community.auth0.com/u/dhaich)\
**Post date:** [March 23, 2022, 2:30am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/15 "2022-03-23T02:30:09Z")

</div>

Hi Thameera. I’m having the same issue. any chance you have a PHP example of your solution?

---

<div class="post-metadata">

**Author:** ![carlosbarrantesb](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/carlosbarrantesb/32/37836_2.png) [@carlosbarrantesb](https://community.auth0.com/u/carlosbarrantesb)\
**Post date:** [June 6, 2022, 4:08pm UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/16 "2022-06-06T16:08:36Z")

</div>

This worked, thank you so much.

---

<div class="post-metadata">

**Author:** ![faisal1](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/faisal1/32/25387_2.png) [@faisal1](https://community.auth0.com/u/faisal1)\
**Post date:** [September 9, 2022, 5:41pm UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/17 "2022-09-09T17:41:47Z")

</div>

This code worked for me,

const formData = new FormData();  
formData.append(“connection\_id”, process.env.AUTH0\_CONNECTION\_ID);  
formData.append(“users”, fs.createReadStream(filePath));  
formData.append(“external\_id”, externalId);

```
    const headers = formData.getHeaders();
    headers.authorization = AUTH0_HEADERS.authorization;
    const response = await axios.post(`${process.env.AUTH0_API_BASE_URL}api/v2/jobs/users-imports`, formData, {
        headers,
    });

```

---

<div class="post-metadata">

**Author:** ![Giannis](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/giannis/32/33281_2.png) [@Giannis](https://community.auth0.com/u/Giannis)\
**Post date:** [October 3, 2022, 9:04am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/19 "2022-10-03T09:04:17Z")

</div>

Hey there, have you found any solution in PHP? I m having the same issue

---

<div class="post-metadata">

**Author:** ![Giannis](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/giannis/32/33281_2.png) [@Giannis](https://community.auth0.com/u/Giannis)\
**Post date:** [October 3, 2022, 11:06am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/20 "2022-10-03T11:06:37Z")

</div>

The following part is working for PHP in case someone needs it

> $cf = new CURLFile(USERS\_IMPORT\_FILE);  
> $ch = curl\_init();  
> curl\_setopt($ch, CURLOPT\_URL, CONNECTION\_URL);  
> curl\_setopt($ch, CURLOPT\_POST, true);  
> curl\_setopt($ch, CURLOPT\_POSTFIELDS, [“users” =\> $cf, “connection\_id” =\> CONNECTION\_ID]);  
> curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, true);  
> $headers[] = "Authorization: Bearer ".MGMT\_API\_ACCESS\_TOKEN;  
> curl\_setopt($ch, CURLOPT\_HTTPHEADER, $headers);
> 
> ```
> $response = curl_exec($ch);
> $err = curl_error($ch);
> curl_close($ch);
> if ($err) {
> echo "cURL Error #:" . $err;
> } else {
> echo $response;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [October 17, 2022, 11:06am UTC](https://community.auth0.com/t/bulk-user-imports-node-js-example-does-not-work/62482/21 "2022-10-17T11:06:39Z")

</div>

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