For Developers

Uploading Files

The core idea of uploading a file, for now, is that the file should be represented as a JSON array of numbers. To be precise, a Uint8Array of the file contents. We have tentative plans to provide other options for upload in the future.

Graph API

Currently, uploading is only possible via the graph API. Many LMSs require multiple requests to upload a file. For the sake of simplicity, we expose a single POST, which will make the metadata and file upload requests behind the scenes. This is why we require both the file metadata and the file contents in a single JSON payload.

Example using JavaScript

Save the following JS code to a file fileAsUint.js:

const fs = require('fs');

// Read the file path from command line arguments
// process.argv[0] is 'node', process.argv[1] is the script path
// so the first actual argument is at index 2
const filePath = process.argv[2];

// Check if a file path was provided
if (!filePath) {
    console.error('Error: Please provide a file path as an argument');
    process.exit(1);
}
try {
    const buffer = fs.readFileSync(filePath);
    // Convert buffer to Uint8Array (though Buffer is already Uint8Array-like)
    const uint8Array = new Uint8Array(buffer);

    // Log the Uint8Array
    console.log(uint8Array);

    // Optional: Log some information about the array
    console.log(`File size: ${uint8Array.length} bytes`);
} catch (error) {
    console.error(`Error reading file: ${error.message}`);
}

Given an example mytestfile.txt with the following contents:

hello

Or, the example mypixel.png that consists of a single completely black pixel in the png format, you can use node to transform a file:

$ node fileAsUint.js mytestfile.txt
Uint8Array(5) [ 104, 101, 108, 108, 111 ]
File size: 5 bytes

$ node fileAsUint.js mypixel.png
Uint8Array(72) [
  137, 80, 78, 71, 13,  10,  26, 10,   0,  0,   0,  13,
   73, 72, 68, 82,  0,   0,   0,  1,   0,  0,   0,   1,
    8,  2,  0,  0,  0, 144, 119, 83, 222,  0,   0,   0,
   15, 73, 68, 65, 84, 120,   1,  1,   4,  0, 251, 255,
    0,  0,  0,  0,  0,   4,   0,  1, 101, 73, 195,  96,
    0,  0,  0,  0, 73,  69,  78, 68, 174, 66,  96, 130
]
File size: 72 bytes

Example data Array Text File

Given an example mytestfile.txt with the following contents:

hello

From a terminal with hexdump installed (most unix terminals), an easy way to get the data field is:

$ echo -n "["; hexdump -v -e '1/1 "%u,"' mytestfile.txt | sed 's/,$//'; echo "]"
[104,101,108,108,111]

Example data Array Image File

Given an example mypixel.png that consists of a single completely black pixel in the png format:

$ echo -n "["; hexdump -v -e '1/1 "%u,"' mypixel.png  | sed 's/,$//'; echo "]"
[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,1,0,0,0,1,8,2,0,0,0,144,119,83,222,0,0,0,15,73,68,65,84,120,1,1,4,0,251,255,0,0,0,0,0,4,0,1,101,73,195,96,0,0,0,0,73,69,78,68,174,66,96,130]