# How to use IPFS in your Dapp?

Author: Markus Waas

Published: 2020-07-12T01:19:24.000Z

Updated: 2026-09-13T14:25:30.000Z

Source: [https://soliditydeveloper.com/ipfs](<https://soliditydeveloper.com/ipfs>)

## Compatibility and review

Before you start

The Solidity example targets 0.6.11; its Kovan deployment is historical. js-IPFS and the ipfs package used below were replaced by Helia, and the Create Eth App frontend needs migration. Content addressing verifies bytes; continued availability requires retained, reachable copies.

[Official reference](<https://docs.ipfs.tech/concepts/what-is-ipfs/>)

You may have heard about [IPFS](<https://ipfs.io/>) before, the Interplanetary File System. The concept has existed for quite some time now, but with IPFS you'll get a more reliable data storage, through content addressing and peer-to-peer protocols. IPFS itself is not a blockchain. Filecoin is a separate network that provides storage incentives.

If you are participating in the [HackFS](<https://hackfs.com/>), a 2020 hackathon sponsored by [ETHGlobal](<https://ethglobal.co/>)and [Protocol Labs](<https://protocol.ai/>) (the makers of IPFS), or not, knowing IPFS will be a useful skill for your Dapp developments.

### Can't you just store all data in the Ethereum blockchain directly?

Excellent question and in theory yes. But remember that data in the Ethereum blockchain is shared between every single node. This is extremely inefficient for large sets of data. One alternative is to keep file contents off-chain and put a content identifier on-chain. IPFS nodes retain content they add, pin or cache; the network does not randomly assign files to storage nodes.

### How does it work?

The core pieces include content identifiers, peer-to-peer transfers and content routing. The Kademlia-based DHT helps find peers advertising content; it does not itself distribute or store the files. Nodes then request blocks from providers.

IPNS provides mutable names that can point to changing content identifiers. Filecoin provides a separate storage market. IPFS is an open protocol, but running nodes or paying a pinning service still has costs. File access is not protected by built-in private file permissions; encrypt confidential content before sharing it.

### Is it secure and reliable?

In short no, but it depends what we mean with 'reliable'. You cannot store a file in IPFS, then try to read it two years later and still expect it to be there. Nodes will garbage collect unused files. Popularity may increase caching, but it is not a retention guarantee. Pin important content on maintained nodes or services, keep backups and test retrieval after the original uploader is offline. And is it secure? Data is all public, so if you are concerned about privacy, you need to encrypt the files.

## Adding IPFS to Solidity contracts

The Solidity part will be very simple. We will only store the IPFS hash of users inside our contract:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.11;

contract IpfsStorage {
    mapping (address => string) public userFiles;

    function setFile(string memory file) external {
        userFiles[msg.sender] = file;
    }
}
```

The linked Kovan deployment belongs to the original 2020 example. Kovan is retired; use a currently supported test network for a fresh deployment, and check the compiler and frontend dependency versions together.

```bash
yarn workspace @project/react-app add ipfs
```

The full Javascript IPFS documentation is available [here](<https://github.com/ipfs/js-ipfs>). Be aware that it's still very much work in progress.

Okay, now let's initialize IPFS. It creates and connects an IPFS node that we can use to read and upload files.

**JavaScript compatibility:** The following snippets are the old js-IPFS API. That project is deprecated. For a new frontend, use [Helia](<https://github.com/ipfs/helia>) and its current content APIs; simply changing the package name does not migrate `IPFS.create`, `node.add` or the CID result shape.

```javascript
import IPFS from "ipfs";

let node; // shared with the historical upload example

async function initIpfs() {
  node = await IPFS.create();
  const version = await node.version();
  console.log("IPFS Node Version:", version.version);
}
```

Now that we have the IPFS node, let's add a function to read the current file from the contract.

We use the [ethers.js](<https://github.com/ethers-io/ethers.js/>) library with our current address.

```javascript
async function readCurrentUserFile() {
  const result = await ipfsContract.userFiles(
    defaultProvider.getSigner().getAddress()
  );

  return result;
}
```

Let's upload a file to IPFS and store it in our contract.

- uploadFile: This will be the function to take a file and upload it to IPFS using our IPFS node.
- `setFile`: After a successful upload, we can store the IPFS hash inside our contract using this function.

```javascript
async function setFile(hash) {
    const ipfsWithSigner = ipfsContract.connect(defaultProvider.getSigner());
    await ipfsWithSigner.setFile(hash);
    setIpfsHash(hash);
}

async function uploadFile(file) {
    const files = [{ path: file.name + file.path, content: file }];

    for await (const result of node.add(files)) {
        await setFile(result.cid.string);
    }
}
```

Now let's add a nice [drag zone](<https://github.com/react-dropzone/react-dropzone>) for the user to upload a file in the browser. And put it all together.

Please check out the live demo and repository for the full example.

- Repository: [https://github.com/gorgos/IPFS-Dapp](<https://github.com/gorgos/IPFS-Dapp>)
- Live Demo: [https://ipfs-dapp.netlify.app/](<https://ipfs-dapp.netlify.app/>)

![Example IPFS App](<https://cdn0.scrvt.com/b095ee27d37b3d7b6b150adba9ac6ec8/e70774a03038b593/e93a1853ae50/v/2270ef709109/example-ipfs-app.png>)

## Some alternatives

- [Swarm](<http://ethswarm.org/>): Very similar to IPFS, you can use it the same way. It has a bigger focus on Ethereum relying on smart contract incentives. However, it's not quite as matured yet.
- [SKALE Filestorage](<https://skale.network/docs/developers/products/file-storage>): Side chains are effectively using a subset of Ethereum nodes allowing for more data to be stored inside them. Skale made use of that and introduced their own file-storage sytem. SKALE was launched to mainet only a few weeks ago, so you can go ahead and try it.

The launch timing and maturity comparisons above describe 2020. They are not current service recommendations or guarantees of data retention.
