Next.js
This guide will walk you through setting up Pinata in a Next.js app.
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.
If you are just getting started we recommend using Admin privileges, then move to scope keys as you better understand your needs
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, be sure to copy them somewhere safe!
After you have your API key you will want to get your Gateway domain. Gateways are the fastest way to fetch content from Pinata, and are the ideal tool when building applications. 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:
Server-side Setup
Next.js has a limit of how large a file can be passed through the built in API routes, if you need to enable larger uploads follow the client side setup guide
Start up Next.js Project
As with any Next.js project we can start one up with the following command
After the project is created cd
into the repo and install pinata
After making the project, create a .env.local
file in the root of the project and put in the following variables:
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 root of the project and then make a file called config.ts
inside of it. In that file we’ll export an instance of the IPFS SDK that we can use throughout the rest of the app.
Create Client Side 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 /app/page.tsx
file take out the boiler plate code and use the following.
This will take a file from the client side and upload it through an API route we are going to make next.
Next.js does have a file size limitation for what can be passed through the API routes, so if you need more than the limit then it is advised to make signed JWTs by folloing this guide.
Create API Route
Next.js is ideal for file uploads as it’s API routes keep keys hidden and unexposed to the client. In the last step we made a function that uploads to /api/files
so now we need to create that route by making /app/api/files/route.ts
in our app.
Once you have created that file you can paste in the following code.
This will accept a POST
request from the client, then send an API request to Pinata with the upload, then make one more request to get a signed URL we can use to see the content. Once complete it will return the URL to the client.
With our URL we can render the image we uploaded by adding the following code to the page.tsx
file.
And just like that we have uploaded an image to Pinata and recieved a usable URL in return!
Client Side Setup
Next.js has a file size limit as to what can be pass through API routes, so another workaround is to upload the file on the client side. To do this securely you can make an API route that generates a temporary Pinata JWT that is used in the upload.
Start up Next.js Project
As with any Next.js project we can start one up with the following command
After the project is created cd
into the repo and install pinata
After making the project, create a .env.local
file in the root of the project and put in the following variables:
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 root of the project and then make a file called config.ts
inside of it. In that file we’ll export an instance of the IPFS SDK that we can use throughout the rest of the app.
Create API Route
In order to upload on the client side we need an API key, but we don’t want to expose our primary admin key. To avoid this we’ll make an API route in our Next project under app/api/key/route.ts
.
Once you have created that file you can paste in the following code.
When the client makes a GET
request to /api/key
it will return a temporary API key that is only valid for pinFileToIPFS
and one single use (maxUses: 1
).
Create Client Side 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 with a temporary key
In the /app/page.tsx
file take out the boiler plate code and use the following.
The main thing to understand here is that we are able to use the key()
method in combination with our upload methods which passes in the temporary key instead of trying to access the admin key.
Now that we have the file uploaded we can create a URL to display in our app.