Request: Assembly API Example with Signature Authentication

I can’t seem to find an example anywhere that covers signature authentication through the API. The NodeJS sdk requires authSecret so I can’t use it.

Every time I try to use the API endpoint directly, I get “INVALID_FORM_DATA”.

This is how I build my formdata. I’ve tried a couple different variations but they all are failing. The lack of examples for signature authentication is very frustrating considering it’s stated as a best practice

    let form = new FormData();
    form.append('file', fs.createReadStream('tests/files/Mountain_Dig_Site.png'));
    form.append('params', response.params),
    form.append('signature', response.signature)


    let transloadit_response = await (await fetch('https://api2.transloadit.com/assemblies', {
      method: 'post',
      headers: {
        'content-type':'multipart/form-data'
      },
      body: form
    })).json()
    console.log(transloadit_response);

Example of the Response Object (certain stuff replaced with ####)

 {
  params: '{"auth":{"key":"####","expires":"2021/03/17 01:45:53+00:00"},"steps":{":original":{"robot":"/upload/handle"},"filter":{"use":":original","robot":"/file/filter","accepts":[["${file.mime}","regex","image"]],"error_on_decline":true},"viruscheck":{"use":"filter","robot":"/file/virusscan","error_on_decline":true},"convert_image_webp":{"use":"viruscheck","robot":"/image/resize","format":"webp","imagemagick_stack":"v2.0.7"},"compress_image":{"use":"convert_image_webp","robot":"/image/optimize","progressive":true},"watermark":{"use":"compress_image","robot":"/image/resize","watermark_url":"https://demos.transloadit.com/inputs/transloadit-padded.png","watermark_size":"25%","watermark_position":"bottom-right","imagemagick_stack":"v2.0.7"},"resize_image":{"use":"watermark","robot":"/image/resize","resize_strategy":"fit","width":100,"height":100,"imagemagick_stack":"v2.0.7"},"export":{"use":["convert_image_webp","compress_image","watermark","resize_image"],"robot":"/backblaze/store","credentials":"####","path":"advl-test/${file.url_name}"}}}',
  signature: '6318bb36d48106fad9086219bb4ce41e7d86eb85'
}

Hi there, I believe the issue that you may be running into is coming from when you’re sending your request. I’ve made a small example in Node.JS that will send a POST request with a signature and file to a template. The important change is that I use the submit function from the form-data library that it appears you’re currently using. Hope this helps!

var FormData = require('form-data');
var fs = require('fs');
const crypto = require('crypto')

const utcDateString = (ms) => {
  return new Date(ms)
    .toISOString()
    .replace(/-/g, '/')
    .replace(/T/, ' ')
    .replace(/\.\d+\Z$/, '+00:00')
}

const expires    = utcDateString((+new Date()) + 1 * 60 * 60 * 1000)
const authKey    = 'AUTH_KEY'
const authSecret = 'AUTH_SECRET'

const params = JSON.stringify({
  auth: {
    key: authKey,
    expires,
  },
  template_id: 'TEMPLATE_ID',
})

const signature = crypto
  .createHmac('sha1', authSecret)
  .update(Buffer.from(params, 'utf-8'))
  .digest('hex')

var form = new FormData();
form.append('params', params);
form.append('signature', signature);
form.append('file', fs.createReadStream('man.jpg'))

form.submit('https://api2.transloadit.com/assemblies', function(err, res) {
  // res – response object (http.IncomingMessage)  //
  res.resume();
  console.log(res.statusCode)  
});

Ah you’re right, the headers on the form needed to be set (I was able to use form.getHeaders() and use my fetch library instead of submit)

It works! Thank you! :smiley:

1 Like