API upload a new file node.js

Hi, I’m trying to create a API to upload a local file with node js, I use this code from

https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file

var request = require('request');
var fs = require('fs');

var postUrl = 'https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=demo';

var filename = 'example_file.txt';

var fileOptions = {
 access: 'PUBLIC_INDEXABLE',
 ttl: 'P3M',
 overwrite: false,
 duplicateValidationStrategy: 'NONE',
 duplicateValidationScope: 'ENTIRE_PORTAL'
};

var formData = {
 file: fs.createReadStream(filename),
 options: JSON.stringify(fileOptions),
 folderPath: 'docs'
};

request.post({
 url: postUrl,
 formData: formData
}, function optionalCallback(err, httpResponse, body){
 return console.log(err, httpResponse.statusCode, body);
});

but I get this error

null 401 {“status”:“error”,“message”:“This hapikey has been invalidated.”,“correlationId”:“9a164839-aa7d-4a26-ab9c-f73b36cce89c”,“category”:“EXPIRED_AUTHENTICATION”,“errors”:[{“message”:“The API key used to make this call has expired.”}]}

Now I want to acces with a private app token but I don’t how to use it. thank u.

Hi @igeorge5,

The reason you are seeing this issue is that API keys have been deprecated and are no long a valid means of authentication. As you’d correctly mentioned - private apps (or public apps using OAuth2) are the preferred approach. In order to do this with a private app, you must first create the private app and obtain an access token. The access token is then simply included as a header in your request as opposed to a query parameter.

You would then need to modify your code to this:

request.post({
 headers: {
 'Authorization': `Bearer ${your_token}`
 },
 url: postUrl,
 formData: formData
}, function optionalCallback(err, httpResponse, body) {
 return console.log(err, httpResponse.statusCode, body);
});