Working Bulk Import Node JS example

I’ve tried the example on the documents (Bulk User Imports), which does not work (as pointed out in May 2021!) and the related community post suggests an alternative solution, which still does not work (Bulk User Imports, Node js example does not work).

When I try and import the same file using curl, it works, but when I use node I get a 500 error with no useful information, just “internal server error”

import axios from “axios”;
import fs from “fs”;
import FormData from “form-data”;

export const importUsers = async (filename: string, jobId: string, config: { domain: string, managementApiToken: string, connectionId: string }) => {
const formData = new FormData()
formData.append(“connection_id”, config.connectionId)
formData.append(“users”, fs.createReadStream(filename))

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

Hi @kevin.richards1,

Thanks for reaching out to the Auth0 Community!

I have tested the Bulk User Import endpoint using Node.js on my end and got a working sample. Could you please try the following code and see how it goes?

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var data = new FormData();
data.append('users', fs.createReadStream('YOUR_FILE_PATH_NAME'));
data.append('connection_id', 'YOUR_CONNECTION_ID');

var config = {
  method: 'post',
  url: 'https://YOUR_DOMAIN/api/v2/jobs/users-imports',
  headers: { 
    'Authorization': 'Bearer YOUR_MANAGEMENT_API_TOKEN', 
    'Content-Type': 'application/json', 
    ...data.getHeaders()
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

I look forward to your update.

Thanks,
Rueben

Yeah, that seems to work. Perhaps you could update the help docs with that example.

Thanks

1 Like