> ## Documentation Index
> Fetch the complete documentation index at: https://documentation.restash.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js

> Learn how to use the Restash Node.js SDK.

## Installation

```bash npm theme={null}
npm i @restash/node
```

```bash pnpm theme={null}
pnpm i @restash/node
```

```bash yarn theme={null}
yarn add @restash/node
```

## Initialize Restash

```typescript lib/restash.ts theme={null}
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](https://restash.io/api-keys).

## Upload a file

```typescript theme={null}
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:

**file** `File | Blob` *required*

The file or blob to upload.

**options** `UploadOptions object`

An object with the following optional properties:

**properties**

**name** `string`

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.

**path** `string`

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.

**metadata** `object`

Key-value pairs of metadata associated with the file. This can be used to store additional information about the file.

A successful upload will respond with the following:

**data** `UploadFileResponseSuccess object`

The information about the uploaded file.

**properties**

**id** `string`

The unique identifier of the uploaded file.

**name** `string`

The name of the uploaded file.

**url** `string`

The URL of the uploaded file.

**size** `number`

The size of the uploaded file in bytes.

**contentType** `string`

The content type of the uploaded file.

**key** `string`

The unique key of the uploaded file.

**metadata** `object | null`

Key-value pairs of metadata associated with the file.

## Retrieve a file

```typescript theme={null}
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:

**properties**

**id** `string`

The unique identifier of the file.

**key** `string`

The unique key of the file.

A successful retrieval will respond with the following:

**data** `RetrieveFileResponseSuccess object`

The information about the retrieved file.

**properties**

**id** `string`

The unique identifier of the uploaded file.

**name** `string`

The name of the uploaded file.

**url** `string`

The URL of the uploaded file.

**size** `number`

The size of the uploaded file in bytes.

**contentType** `string`

The content type of the uploaded file.

**key** `string`

The unique key of the uploaded file.

**metadata** `object | null`

Key-value pairs of metadata associated with the file.

## Delete a file

```typescript theme={null}
import { restash } from "@/lib/restash";

const result = await restash.files.delete("file-id");
```

The delete method accepts the following parameters:

**options** `DeleteFileOptions object`

An object with the following properties, either id or key is required:

**properties**

**id** `string`

The unique identifier of the file.

**key** `string`

The unique key of the file.

A successful deletion will respond with the following:

**data** `DeleteFileResponseSuccess object`

A confirmation of the deletion.

**properties**

**deleted** `boolean`

A boolean indicating if the file was deleted successfully.

**file** `string`

The unique identifier of the file.

## Additional resources

> **[NPM package](https://www.npmjs.com/package/@restash/node)**
>
> Download and install the Restash Node.js SDK from NPM

> **[Source code](https://github.com/restashio/restash-node)**
>
> View the complete source code for the Restash Node.js SDK


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.