File Upload

Upload files with Story Protocol SDK

Files upload allows users to upload images, metadata and other data onto decentralized file systems. In Story Protocol, the SDK uses Arweave for hosting the uploaded files. Developers can use the uploadFile function in PlatformClient to upload files with SDK.

Usage

const { txHash } = await client.platform.uploadFile(file, mimeType);

Parameters

  • file: File | Buffer: the object represents the file in typescript. For frontend development, we can use the object of File type in Javascript/Typescript. For backend development or pure node, we can use the object of Buffer type.
  • mimeType: string: the media type (also known as a Multipurpose Internet Mail Extensions or MIME type) which indicates the format of the file. You can refer to this link for the full list of MIME types.

Response

Success Response

  • uri: string: the string represent the URI of the uploaded file on Arweave.

Failure Response

The 500 server error will returned when:

  • The uploaded file is too big.

Example

In Node, we can use the following code to upload a local file story.png with uploadFile function:

import { readFileSync } from 'fs';

const file = readFileSync(__dirname + '/story.png');

// Upload file
client.platform.uploadFile(file, 'image/x-png').then(uri => {
  console.log(uri);
});

By running above example, you will get the following output:

{
  uri: 'https://arweave.net/m-3wkEOwmCwfkv1A25qgo8WS3O3t8a9-e-vi5vrejWE'
}

You can also refer to the full source code of above example at https://github.com/storyprotocol/my-story-protocol-example/tree/main/sdk/platform.