Installation

npm i @restash/client

Basic usage

Here’s a simple example of how to use the Restash client SDK to upload files directly from the browser.

import { createRestashUploader } from "@restash/client";

// create the uploader with your public API key
const upload = createRestashUploader({
  publicKey: "pk_123456789",
});

// can be a file or a blob
const file = new File(["test"], "test.txt", {
  type: "text/plain",
});

const result = await upload(file);

console.log(result.url); // https://cdn.restash.io/{result.key}

For more usage examples, check out:

Creating the uploader

The createRestashUploader function creates an uploader instance.

lib/restash.ts
import { createRestashUploader } from "@restash/client";

export const upload = createRestashUploader({
  publicKey: "pk_123456789",
});

It accepts an options object with the following properties:

publicKey
string
required

Your Restash public API key. This is required to authenticate the upload request.

handleSignature
string

The URL of your server route that returns a signature and payload. Only required if your team has signature enforcement enabled. Learn more.

Using the uploader

You can import your uploader instance and use it to upload files.

import { upload } from "@/lib/restash";

const result = await upload(file, {
  path: "uploads/",
  onProgress: ({ percent }) => {
    console.log(`uploading...${percent}%`);
  },
});

It accepts the following parameters:

file
File | Blob
required

The file or blob to upload.

options
UploadOptions object

An object with the following optional properties:

Generating signatures

If your team has signature enforcement enabled, you need to generate a signature for each upload request. You can do this by creating a server route that returns a signature and payload using the generateSig function from the Restash client SDK. Generating signatures needs to be done on the server. You can then pass this route to the createRestashUploader so that it can be called when needed.

/api/restash/signature/route.ts
import { NextResponse } from "next/server";
import { generateSig } from "@restash/client";

export const GET = () => {
 const secret = process.env.RESTASH_SECRET_KEY!;
 const { signature, payload } = generateSig(secret);

 return NextResponse.json({ signature, payload });
}

It accepts the following parameters:

secret
string
required

Your Restash secret API key. This is required to generate the signature.

Additional resources