Upload to Pinata IPFS with React Native

A reference post for uploading files to Pinata IPFS in react native.

A reference post for anyone looking to upload files to Pinata IPFS in a react native project.

Looking at pinata docs, there aren't any examples of how to upload a file to IPFS in react native applications.

If this has been helpful to you, please let me know on twitter or don't feel shy about buying me a coffee or starring this repo on github

const publicIpfsUrl = "https://api.pinata.cloud/pinning/pinFileToIPFS";
const privateIpfsUrl = "https://uploads.pinata.cloud/v3/files";
async function upload() {
  try {
    const fileUri = `${FileSystem.documentDirectory}<filename>.txt`;
    await FileSystem.writeAsStringAsync(fileUri, "Hello World a bigger test!");
    // or publicIpfsUrl
    const res = await FileSystem.uploadAsync(privateIpfsUrl, fileUri, {
      httpMethod: "POST",
      uploadType: FileSystem.FileSystemUploadType.MULTIPART,
      fieldName: "file",
      mimeType: "text/plain",
      headers: {
        Authorization: `Bearer ${EXPO_PUBLIC_PINATA_JWT}`,
      },
    });
  } catch (error) {
    if (error instanceof Error) {
      console.error("Upload error:", error.message);
    }
  }
}