Upload document using axios with DriveWealth API

DriveWealth has an API for managing an investment account. With the API, we can buy / sell US stocks. I am testing the API with a Node / Mocha test client using axios. While most of the API is straight-forward to test, I found just one method which is a bit tricky. Tricky enough to write about it. The tricky API method is uploading a KYC document.

In the past, I have used a file upload component in the browser to upload a document. With the new FormData API, we can upload documents using AJAX. Now, I want to upload a document from Node using the DriveWealth API.

Upload document using axios

The endpoint for document upload is http://api.drivewealth.io/v1/documents. It accepts three parameters: userID, documentType and documentImage. documentImage is a file. In the browser, we have access to FormData API. In Node, we have to install a NPM package form-data.

yarn add form-data

Import form-data

import FormData from 'form-data';

Prepare the FormData as follows.

const formData = new FormData();
formData.append('token', global.userID);
formData.append('documentType', 'Picture ID');
formData.append('documentImage', fs.createReadStream('./files/picture.jpg'));

Send a multipart/form-data using axios.

return axios.post(`http://api.drivewealth.io/v1/documents`, formData, {
        headers: {
            'x-mysolomeo-session-key': global.sessionKey,
            'content-type': 'multipart/form-data'
        }
    })
    .then(response => {
        expect(response.status).to.equal(200);
    });

formData is the second parameter to the POST method. The third parameter is the config object. Here, we pass the headers. The content type is multipart/form-data. We also pass the session key. We retrieve the session key in another API call.

Create user session for using DriveWealth API

The endpoint for creating a new user session is http://api.drivewealth.io/v1/userSessions. There are a lot of parameters it expects. Mostly, details about the client which is accessing the API. The two important pieces of information for creating the session is the username and the password.

return axios.post("http://api.drivewealth.io/v1/userSessions", {
    appTypeID: "2000",
    appVersion: "0.1",
    username: "vijay",
    emailAddress: "vijay@vijayt.co",
    ipAddress: "1.1.1.1",
    languageID: "en_US",
    osVersion: "iOS 9.1",
    osType: "iOS",
    scrRes: "1920x1080",
    password: "passw0rd"
}).then(response => {
    console.log('created session');
    global.sessionKey = response.data.sessionKey;
    global.userID = response.data.userID;
    global.accountID = response.data.accounts[0].accountID;
});

When we get a response, we store the session key, userID and accountID for future API calls. Typically, creating a session is called once before all the test runs.

Related Posts

One thought on “Upload document using axios with DriveWealth API

  1. Thank you for the post. It might be useful to some readers -like me!- to point out that to make FormData work with axios _in node.js_ (as opposed as in the browser), one needs to use the headers as supplied from FormData.getHeaders(). One can always add to these headers, for example to specify the ‘x-mysolomeo-session-key’, but somehow failure to use these prepared headers, results in bad requests on the receiving end.

    See for example, https://github.com/axios/axios/issues/1006#issuecomment-320165427

Leave a Reply

Your email address will not be published.