It Will Uploads Your Creds To Mega And Will Sends You Id Of That File.
How Session Id Will Works?
Click Here To View?
import { fileURLToPath } from 'url';
import path from 'path';
import { writeFileSync } from 'fs';
import * as mega from 'megajs';
// This imports everything from the `megajs` module (which is a JavaScript library to interact with Mega.nz) as an object `mega`.
// This module allows interacting with files stored on the Mega cloud storage.
async function SaveCreds(txt) {
// Declares an asynchronous function named `SaveCreds` that takes `txt` as an argument. The function will save credentials ( in JSON format) to a local file.
const __filename = fileURLToPath(import.meta.url);
// `import.meta.url` gives the URL of the current module. The `fileURLToPath` function converts that URL to a file path for the current file.
const __dirname = path.dirname(__filename);
// `path.dirname` extracts the directory name from the `__filename` path, so it provides the path to the directory containing the current file.
const megaCode = txt.replace('', '');
//if you did used some prefix before the session id
const megaUrl = `https://mega.nz/file/${megaCode}`;
// Creates a Mega URL using the `file id` . It constructs the full URL to access the file stored on Mega.
console.log(megaUrl);
// Logs the generated Mega URL to the console for debugging or confirmation purposes.
const file = mega.File.fromURL(megaUrl);
// Uses the `mega.File.fromURL` method from `megajs` to create a `file` object from the Mega URL. This object represents the file to be downloaded.
try {
const stream = file.download();
// Downloads the file from Mega as a stream. This returns a readable stream of the file's data.
let data = '';
// Initializes an empty string `data` to accumulate the chunks of data downloaded from the stream.
for await (const chunk of stream) {
// Iterates over each chunk in the stream asynchronously (i.e., handles the data as it is downloaded).
data += chunk.toString();
// Converts each chunk (which may be a Buffer) to a string and appends it to the `data` variable.
}
const credsPath = path.join(__dirname, '..', 'session', 'creds.json');
// Joins several path segments to form the path to save the credentials file (it goes up one directory level and then to `session/creds.json`).
writeFileSync(credsPath, data);
// Writes the `data` (credentials) to the `creds.json` file synchronously at the specified `credsPath`.
console.log('Saved credentials to', credsPath);
// Logs a message to the console indicating that the credentials were successfully saved to the specified path.
} catch (error) {
// If an error occurs during the download or file writing process, this block catches it.
console.error('Error downloading or saving credentials:', error);
// Logs the error message to the console, providing feedback if something goes wrong.
}
}
export default SaveCreds;
// Exports the `SaveCreds` function as the default export of this module, making it available for use in main file.
//Now Import Function In Main File
dotenv.config()
import SaveCreds from './some-file.js'
async function main() {
const txt = process.env.SESSION_ID
if (!txt) {
console.error('Environment variable not found.')
return
}
try {
await SaveCreds(txt)
console.log('process SaveCreds completed.')
} catch (error) {
console.error('Error:', error)
}
}
main()
// Now Use Further code
CRAFTED USING TEMPLATES OF SUHAILTECHINFO ( QR ) AND PRABATH ( PAIR )
BOTH PAIR CODE AND QR CODE WORKING
YOU CAN DEPLOY IT ON ANY CLOUD PLATFORM e.g HEROKU
RENDER
KOYEB
etc.
⭐ THE REPO IF YOU ARE GOING TO COPY OR FORK
Note: Make Sure Add Your Email And Password ( Required In mega.js ) Before Running/Deploying The API.
![]() |
---|
Qasim Ali |