React
Since React is a client side framework it is recommend to handle uploads using Presigned URLs using a server side framework like Hono. We would highly recommend following the Hono guide below before doing this React guide!
Hono Guide
Installation
Get Gateway URL
Before we start you’ll need your Dedicated Gateway domain. When you create a Pinata account, you’ll automatically have a 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:
Start up React Project
Run the command below to make a new React project:
Give the project a name and select the React framework. Then cd
into the project and install pinata
.
After making the project, create a .env.local
file in the root of the project and put in the following variable:
The VITE_SERVER_URL
is the URL of your server endpoint that will return Presigned URLs. If you don’t have one be sure to follow the Hono guide first. If you did follow it then you’ll remember it was http://localhost:8787
and we’ll need to make sure it’s running in order for our flow to work.
The format of the Gateway domain should be mydomain.mypinata.cloud
.
Implementation
Now that we have our initial setup, open the src/App.tsx
file and replace it with the following code.
In our code we have a pretty simple form that will trigger handleSubmit()
. This function will do the following:
- Make a request to our server for a Presigned URL (you will want to handle any server side auth here)
- Use the returned Presigned URL in a client side upload
- Use the upload response to create a link to the file
To try it out make sure your server is running, then in another terminal Run
Open up the site on http://localhost:5173
and upload a file!