NPM SDK Integration
Learn how to integrate Ziqx Drive into your project using the official
@ziqx/driveSDK for both Node.js (server) and browser (client) environments.
Overview#
The @ziqx/drive SDK provides a simple, secure way to generate signed URLs and upload files without manually handling the HTTP API. It is written in TypeScript and fully typed, supporting modern JavaScript frameworks like Node.js, Next.js, and React.
The SDK exposes two main classes:
- ZDrive โ Server-side class to generate signed upload URLs.
- ZDriveClient โ Browser-side class to upload files using signed URLs.
Installation#
# Using npmnpm install @ziqx/drive
# Using yarnyarn add @ziqx/drive1. Server-side: Generate Signed URLs (ZDrive)#
The ZDrive class is used server-side to securely generate signed URLs for clients to upload files.
Example#
import { ZDrive } from "@ziqx/drive";
async function generateSignedUrl() { const drive = new ZDrive("your-drive-key", "your-drive-secret");
const signed = await drive.generatePutUrl("example.jpg");
if (signed.success && signed.url) { console.log("โ
Signed URL:", signed.url); } else { console.error("โ Error generating URL:", signed.message); }}
generateSignedUrl();Notes:
driveSecretshould never be exposed on the client.- This signed URL is short-lived and secure.
- Always include the file extension (e.g., .jpg, .png, .pdf). Omitting it may cause the uploaded file to appear broken. The easiest way is to use the name from the file selected by the user, for example:
red-apple.jpg.
2. Client-side: Upload Files (ZDriveClient)#
The ZDriveClient class is used in the browser to upload files using signed URLs obtained from the server.
Example#
import { ZDriveClient } from "@ziqx/drive";
async function handleUpload(file: File, signedUrl: string) { const client = new ZDriveClient(); const response = await client.uploadFile(signedUrl, file);
if (response.success) { console.log("โ
File uploaded:", response.filename); } else { console.error("โ Upload failed:", response.message); }}
// File input exampleconst fileInput = document.getElementById("fileInput") as HTMLInputElement;fileInput.addEventListener("change", async () => { if (fileInput.files?.length) { const signedUrl = await fetch("/api/drive/sign-url") .then((res) => res.json()) .then((r) => r.url);
await handleUpload(fileInput.files[0], signedUrl); }});Notes:
- Always use signed URLs from your server.
- Supports
<input type="file">, drag-drop, or any browserFileobject.
API Reference#
ZDrive (Server-side)#
| Method | Description | Parameters | Returns |
|---|---|---|---|
generatePutUrl(fileName: string) | Generates a signed URL for a file | fileName โ File name | Promise<SignUrlResponse> |
ZDriveClient (Client-side)#
| Method | Description | Parameters | Returns |
|---|---|---|---|
uploadFile(uploadUrl: string, file: File) | Uploads a file to Ziqx Drive | uploadUrl โ Signed URLfile โ Browser File object | Promise<UploadResponse> |
Response Types#
SignUrlResponse#
interface SignUrlResponse { success: boolean; message: string; url?: string;}UploadResponse#
interface UploadResponse { success: boolean; message: string; filename?: string; driveid?: string;}Best Practices#
- Keep
driveSecretserver-side only. - Validate file names and types before uploading.
- Always check
successbefore using the returned URL or file name.
License#
MIT ยฉ ZIQX