Remix
Get started using Pinata with Remix
Startup Remix Project
We can start a Remix app by entering the command below into the terminal
After it’s finished installed we’ll want to cd
into the project and install the pinata
SDK.
Now let’s make a file called .env
file in the root of the project with the following variables:
Setup Pinata
Make a folder in the root of the project called utils
, and inside make a file called pinata.ts
with the following code:
This will create an instance of the SDK we can use throughout the app.
Upload Form and Server Action
In the app/routes
directory we can remove the boilerplate code and replace it with the code below.
Let’s walk through what’s happening here. To start we have our client side markup:
This is a simple Form
element provided by Remix which lets us make a submission to a server action. The most important piece to notice here is the encType="multipart/form-data"
, which is crucial to make file / multipart requests work in Remix!
Then we have our server action:
This will be the default endpoint used when we submit our form. It will parse the incoming formData
, get the file
attached, and then we upload it to Pinata using pinata.upload.file()
. From that method we deconstruct the result and return the IpfsHash
which we can use in convert
. This will make a URL we can return to the client and make accessible to the user, and in our case the <a>
tag that links to the content