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!
Setup Astro
To create a new Astro project go ahead and run this command in the terminal:cd into the repo and install pinata.
vercel and svelte for this tutorial, and you can install them like so.
astro.config.mjs for us.
Setup Pinata
In thesrc folder make a new folder called utils, and inside there make a file called pinata.ts with the following contents:
src/utils/pinata.ts
.env with the following values:
Create Client Side Form
In thesrc folder make another folder called components, then inside there make file called UploadForm.svelte.
src/components/UploadForm.svelte
<form> with a file <input> as well as a <button> to send it. The form will trigger the submit function, which takes our form data and makes an API request to our server side code. Once complete the API will send response with a url that we can use in the <img> tag to display it on the page.
Server Side API Route
In thesrc/pages folder make a new folder called api and inside there make a file called upload.ts. Paste the following code inside it:
formData and get the file we attached. If there isn’t a file attached we’ll send a error response. Otherwise we’ll upload the file using the SDK method pinata.upload.public.file and deconstruct the response to get the cid. With that cid we can use the convert method to create a URL, which we can then send back to the client.
Add Component to the Page
The last step here is to update thesrc/pages/index.astro file with our new component!
src/pages/index.astro