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.

Import form-data

Prepare the FormData as follows.

Send a multipart/form-data using axios.

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.

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.

Upload document using axios with DriveWealth API
Tagged on: