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
The API keys are only shown once, be sure to copy them somewhere safe!
Server-side Setup
This guide will walk you through a server side upload flow in SvelteKitStart up SvelteKit Project
The easiest way to start building a SvelteKit app is to runnpm create
cd into the repo and install pinata
tailwindcss with typography plugin. Follow other CSS Framework guides respectively.
.env.local file in the root of the project and put in the following variables:
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 calledserver in the src/lib folder of the project and then make a file called pinata.ts inside of it. In that file we’ll export an instance of the Files SDK that we can use throughout the rest of the app.
The use of the
server directory prevents it being used client side.src/lib/server/pinata.ts
Create Client Side Form
Next we’ll want to add a form on our home page that will allow someone to select a file and upload it. In thesrc/routes/+page.svelte file take out the boiler plate code and use the following.
Lets also restrict the allowed extensions for the file. (e.g. jpg, jpeg, png, webp).
src/routes/+page.svelte
Create Server Action
Create a server file,src/routes/+page.server.ts, so that we can add a form action to use.
Since this demo and page only has 1 form, we will keep the default action.
src/routes/+page.server.ts
Access the Returned value of the Form Action
We can access the returned data by exporting theform prop on our +page.svelte. Now, after submitting the form,
the form prop will be updated and we can conditionally render the image we just uploaded.
src/routes/+page.svelte