If you want to add Pinata to a React project that is client side only you can do so, however it must be stated that using your Pinata API keys in a client side app means they will be exposed! This approach is not secure and it is recommneded to either scope the API keys to certain permissions or use signed JWTs from a server.

It is still highly recommend using the Next.js Quickstart as it is much more secure with server side API routes and works similar to React.

Using Pinata API keys in a React app will expose them, proceed with caution!

Installation

Create an API Key and get Gateway URL

To create an API key, visit the Keys Page and click the “New Key” button in the top right. Once you do that you can select if you want your key to be admin or if you want to scope the privileges of the keys to certain endpoints or limit the number of uses. Make those selections, then give the key a name at the bottom, and click create key.

We highly encourage scoping keys if you are planning to expose them in a client side environment

Once you have created the keys, you will be shown your API Key Info. This will contain your Api Key, API Secret, and your JWT. Click “Copy All” and save them somewhere safe!

The API keys are only shown once, so be sure to copy them somewhere safe!

After you have your API key, you will want to get your Dedicated Gateway domain. Dedicated Gateways are the fastest way to fetch content from IPFS, and are the ideal tool when building decentralized applications. When you create a Pinata account, you’ll automatically have a Dedicated Gateway created for you! To see it, simply visit the Gateways Page see it listed there.

The gateway domains are randomly generated and might look something like this:

aquamarine-casual-tarantula-177.mypinata.cloud

Start up React Project

Run the command below to make a new React project:

npm create vite@latest

Give the project a name and select the React framework. Then cd into the project and install pinata.

npm i pinata

After making the project, create a .env.local file in the root of the project and put in the following variables:

VITE_PINATA_JWT=
VITE_GATEWAY_URL=

Use the JWT from the API key creation in the previous step as well as the Gateway Domain. The format of the Gateway domain should be mydomain.mypinata.cloud.

Setup Pinata

Create a directory called utils in the src folder of the project and then make a file called config.ts inside of it. In that file we’ll export an instance of the Pinata SDK that we can use throughout the rest of the app.

src/utils/config.ts
import { PinataSDK } from "pinata"

export const pinata = new PinataSDK({
  pinataJwt: `${import.meta.env.VITE_PINATA_JWT}`,
  pinataGateway: `${import.meta.env.VITE_GATEWAY_URL}`
})

Create Upload Form

Next we’ll want to make an upload form on the client side that will allow someone to select a file and upload it.

In the src/App.tsx file take out the boiler plate code and use the following. Switch between tabs if you want to see how you would handle folder uploads.

Uploading files like this will expose your API keys!

Pull Content from IPFS

After we have uploaded the file and get the CID back we can create a signed URL for the file to either download it or render it in our app. For this example we will assume the file is an image, but Pinata supports any kind of file type.

We can do this by adding the following code to our App.tsx file.

src/App.tsx
import { useState } from "react";
import { pinata } from "./utils/config"

function App() {
  const [selectedFile, setSelectedFile]: any = useState();
  const [url, setUrl]: any = useState();

  const changeHandler = (event: any) => {
    setSelectedFile(event.target.files[0]);
  };

  const handleSubmission = async () => {
    try {
      const upload = await pinata.upload.file(selectedFile)
      console.log(upload);

      const signedUrl = await pinata.gateways.createSignedURL({
          cid: upload.cid,
          expires: 30
      })
      setUrl(signedUrl)
    } catch (error) {
      console.log(error);
    }
  };

  return (
    <>
      <label className="form-label"> Choose File</label>
      <input
        type="file"
        onChange={changeHandler}
      />
      <button onClick={handleSubmission}>Submit</button>
      {url && (
        <img
          src={url}
          alt="uploaded image"
        />
      )}
    </>
  );
}

export default App;