Installation
Initialize Restash
import { Restash } from "@restash/node" ;
export const restash = new Restash ( process . env . RESTASH_SECRET_KEY );
The Restash
class accepts a single parameter, which is your private API key. You can create an API key here .
Upload a file
import { restash } from "@/lib/restash" ;
const file = new File ([ "test" ], "test.txt" , {
type: "text/plain" ,
});
const result = await restash . files . upload ( file );
// do something with the uploaded file
const { url } = result ;
The upload method accepts the following parameters:
The file or blob to upload.
An object with the following optional properties:
A custom name for the file. This will override the original file name. If a blob is passed and no name is provided, we will generate a random name.
Prefix the file name with a custom path. This is useful for organizing files in your Restash account. You can think of this as a folder structure.
A successful upload will respond with the following:
data
UploadFileResponseSuccess object
The metadata of the uploaded file.
The unique identifier of the uploaded file.
The name of the uploaded file.
The URL of the uploaded file.
The size of the uploaded file in bytes.
The content type of the uploaded file.
The unique key of the uploaded file.
Retrieve a file
import { restash } from "@/lib/restash" ;
// can be a file id or key
const file = await restash . files . retrieve ({ id: "file-id" });
The retrieve method accepts the following parameters:
options
RetrieveFileOptions object
An object with the following properties, either id or key is required:
The unique identifier of the file.
The unique key of the file.
A successful retrieval will respond with the following:
data
RetrieveFileResponseSuccess object
The metadata of the uploaded file.
The unique identifier of the uploaded file.
The name of the uploaded file.
The URL of the uploaded file.
The size of the uploaded file in bytes.
The content type of the uploaded file.
The unique key of the uploaded file.
Delete a file
import { restash } from "@/lib/restash" ;
const result = await restash . files . delete ( "file-id" );
The delete method accepts the following parameters:
An object with the following properties, either id or key is required:
The unique identifier of the file.
The unique key of the file.
A successful deletion will respond with the following:
data
DeleteFileResponseSuccess object
A confirmation of the deletion.
A boolean indicating if the file was deleted successfully.
The unique identifier of the file.
Additional resources