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.
- Node ESM
- Node CJS
- Browser
- Deno
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)
const { Storage } = require('megajs')
// Node doesn't support top-level await when using CJS
;(async function () {
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)
}()).catch(error => {
console.error(error)
process.exit(1)
})
import { Storage } from 'https://unpkg.com/megajs/dist/main.browser-es.mjs'
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)
import { Storage } from 'npm: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.
- Node ESM
- Node CJS
- Browser
- Deno
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
const { File } = require('megajs')
// Node doesn't support top-level await when using CJS
;(async function () {
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
}()).catch(error => {
console.error(error)
process.exit(1)
})
import { File } from 'https://unpkg.com/megajs/dist/main.browser-es.mjs'
const file = File.fromURL('https://mega.nz/file/example#example')
file.api.userAgent = null
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
import { File } from 'npm: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
- This is based on tonistiigi's mega library.
- This is all unofficial, based on developer guide and site source.
- Make sure you agree with MEGA's Terms of Service before using it.