Skip to main content

MEGAJS

UnofficialJavaScriptSDK for MEGA

Quick start

To upload files to MEGA import the library using either ES modules or CommonJS.

Then create a storage object to login using your e-mail and password, then wait for the "ready" event.

Finally you can call the .upload() method to upload files to your account.

import { Storage } from 'megajs'

const storage = await new Storage({
email: 'user@example.com',
password: 'correct horse battery example'
}).ready

const file = await storage.upload('hello-world.txt', 'Hello world!').complete
console.log('The file was uploaded!', file)

Downloading files

To download shared files then you first open a file object using the File.fromURL() method.

Then you load file attributes such as name and size using .loadAttributes().

Finally use the .downloadBuffer() method to download the file which will return a Buffer with the file contents.

import { File } from 'megajs'

const file = File.fromURL('https://mega.nz/file/example#example')

await file.loadAttributes()
console.log(file.name) // file name
console.log(file.size) // file size in bytes

const data = await file.downloadBuffer()
console.log(data.toString()) // file contents