Using Files

Build has first class support for a file type. It standardizes the way that creators deal with files their users send them, as well as making it really easy for consumers to send files from a myriad of different sources. For more information on how to send a file to Build, visit Sending Files.

File Type

When a file is sent to your service, it will always be in the following format.

{
    filename: file.png, // Name of the file (if known)
    type: 'png', // Type of the file
    file: BUFFER // File in javascript buffer
  }

Responding with files

Any type of file passed to api.success will automatically be converted to the same file type for the consumer.

Example Service

Below is the code for a service that resizes the image passed in to the specified width.

const jimp = require('jimp'); 

/*
  * Shrinks image to 50px
  *
  * @param {file} image Image
  * @param {number} width=30 Width of the file
  * @throws {ValidationError} Missing required param
  * @returns {file} The shrunk file
*/
module.exports = (data, api) => {
  jimp.read(data.image.file, (err, image) => {
    if(err) api.error(err);
    image.resize(data.width, jimp.AUTO);
    image.getBuffer(jimp.AUTO, (err, buffer) => {
      if(err) api.error(err);
      api.success(buffer);
    })
  });
};