In part 1 of this series, we took a tour of IPFS, explored its significance, and explained how it works. Now, we'll show you how to get started with Infura’s IPFS service, including a tutorial on how to use our IPFS API to pin your data across the IPFS network, as well as upload and access files.
To get started with IPFS, you need to run a version of the IPFS daemon, or have access to a remote IPFS node. Now, there may be a number of reasons where you don't want to run a local IPFS node, neither installed nor a docker.
So, how do you pin your data across the IPFS network via the Infura API? It’s much easier than you think! Once you have an Infura account (you can sign up for one here), you will be able to upload your file to IPFS and access that data. Data is currently pinned until it’s been 6 months since it was last used, so as long as you’re accessing your data within that time frame, it’ll be there for you!
There are many ways to upload a file to IPFS, including going directly through IPFS locally installed on your machine or through the Infura API. For this tutorial, let’s go through how to upload your file using Python and the Infura API. There are a couple ways to do this - we’ll show you the manual way step-by-step, and will provide you with some resources for using the API wrapper for when you start to do more complicated things:
The first thing you need to do is make sure Python is downloaded on your machine (you can download it here), as our file is written in Python. Next, you’ll want to ensure you import
Next, we want to identify the file we are uploading, as well as its contents, to be uploaded to IPFS. For our example, let’s do something simple and easy to check to make sure it’s working correctly:
Now for the fun part: actually uploading our file. To do this, you’ll want to send a POST request to the Infura IPFS API that includes the files you created above. Once your file gets uploaded, we will want to see the hash that was generated so we can easily call this file to see it later. To do that, we need to take the POST response and get the hash out of it, and then print that hash:
When you run the above code
python3 <filename>.py, if all goes successfully, you’ll see something like the below in your terminal as a printout:
Ta da! You now have the hash that’s required to access your file via the Infura IPFS API.
If you want to go the API wrapper route, you have a few choices, all with great documentation that should help you get set up quickly. Again, these wrappers allow you to complete more complex tasks more efficiently than using the above code, once you’re at that point:
Now we'll show you how to actually access your file.
You’re going to want to create a new Python file and import requests again at the top. This time, we are going to be using the
get function from the Infura documentation to be able to read what’s in our file we just uploaded via the Infura API.
To do that, we will need to declare our parameters, which in this case is the hash we found in the last step:
This allows us to put the hash in the proper location in our request we’re about to call to download the file and its contents. The next bit of code is very similar to what we did to add our file to IPFS:
At this point, if we print out our response, we’ll see a 200 status code, which tells us that we have successfully completed the request and can access the data contained in the response we have. For this tutorial, we want our message from the uploaded file to be printed out to the terminal - to do this, we simply
Now, when you look at your terminal, you should see the message we uploaded in our first step of the tutorial:
You’ve now successfully uploaded and accessed a file through the Infura IPFS API - congrats!
Check out our documentation for a full list of our current supported functionality and the associated urls you can use to implement the call of your choice:
We currently have a 100mb single upload limit. We are working on a premium IPFS offering that will allow advanced usage such as larger file uploads. This product will also expand on the current API functionality, provide a full dashboard and the latest auth integration, and offer an overall more reliable and scalable solution for developers with production ready applications. We will have much more on this in the coming months.
Subscribe to our newsletter so you can be the first to know when our new IPFS product has launched. As always, if you have questions or feature requests, you can join our community or reach out to us directly.