Media POST

Upload images and files to your Datasquirel media storage programmatically.

Overview

Use the Media POST endpoint to upload one or more files to your media storage. Files are stored in the folder you specify, and image thumbnails are generated automatically.

npm Package

import datasquirel from "@moduletrace/datasquirel";
import fs from "fs";

// Read the file and convert to base64
const fileBuffer = fs.readFileSync("./photo.jpg");
const base64 = fileBuffer.toString("base64");

const result = await datasquirel.media.add({
    media: [
        {
            name: "photo.jpg",
            base64: `data:image/jpeg;base64,${base64}`,
        },
    ],
    folder: "profile-images",
    type: "image",
    apiKey: process.env.DATASQUIREL_API_KEY,
});

console.log(result.payload); // Uploaded media object(s)

Parameters

ParameterTypeRequiredDescription
mediaarrayYesArray of files to upload. Each item has a name and base64 string
folderstringNoDestination folder name. Created automatically if it does not exist
typestringYesFile type — "image", "video", "audio", or "file"
apiKeystringNoAPI key. Falls back to DATASQUIREL_API_KEY

media Array Items

FieldTypeDescription
namestringThe file name including extension (e.g. "photo.jpg")
base64stringBase64-encoded file data. Must include the data URL prefix (e.g. data:image/jpeg;base64,...)

REST API

POST /api/v1/media

Headers:

Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

Body:

{
    "media": [
        {
            "name": "photo.jpg",
            "base64": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ..."
        }
    ],
    "folder": "profile-images",
    "type": "image"
}

Response

{
    "success": true,
    "payload": [
        {
            "id": 23,
            "name": "photo.jpg",
            "folder": "profile-images",
            "url": "https://your-instance.com/media/profile-images/photo.jpg"
        }
    ]
}

Notes

  • A Full Access API key is required. Read-only keys cannot upload files.
  • Image thumbnails are generated automatically and stored alongside the original.
  • Uploading a file with the same name as an existing file in the same folder will overwrite it if update: true is passed.