IPFS
Moralis Has Native Support for IPFS!

Saving Files

โ€‹IPFS is supported out of the box when using Moralis.
You can upload files with the saveIPFS() method (max file size 1 GB).
1
// Save file input to IPFS
2
const data = fileInput.files[0];
3
const file = new Moralis.File(data.name, data);
4
await file.saveIPFS();
5
โ€‹
6
//console.log(file.ipfs(), file.hash())
7
โ€‹
8
// Save file reference to Moralis
9
const jobApplication = new Moralis.Object("Applications");
10
jobApplication.set("name", "Satoshi");
11
jobApplication.set("resume", file);
12
await jobApplication.save();
13
โ€‹
14
// Retrieve file
15
const query = new Moralis.Query("Applications");
16
query.equalTo("name", "Satoshi");
17
query.find().then(function ([application]) {
18
const ipfs = application.get("resume").ipfs();
19
const hash = application.get("resume").hash();
20
console.log("IPFS url", ipfs);
21
console.log("IPFS hash", hash);
22
});
Copied!
The data is automatically pinned.

Saving Objects

You can also upload JSON objects directly from JavaScript, by saving the base64 string, Moralis will automatically create the buffer from the base64 provided:
1
const object = {
2
key: "value",
3
};
4
const file = new Moralis.File("file.json", {
5
base64: btoa(JSON.stringify(object)),
6
});
7
await file.saveIPFS();
Copied!
By uploading base64, you could also upload other base64 encoded files such as images.
JS
React
1
const image = "data:image/png;base64,iVBORw0KGgoAAA....";
2
const file = new Moralis.File("image.png", { base64: image });
3
await file.saveIPFS();
Copied!
1
const { saveFile } = useMoralisFile();
2
โ€‹
3
const uploadFile = () => {
4
const base64 = "V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=";
5
saveFile(
6
"myfile.txt",
7
{ base64 },
8
{
9
type: "base64",
10
saveIPFS: true,
11
onSuccess: (result) => console.log(result.ipfs()),
12
onError: (error) => console.log(error),
13
}
14
);
15
};
Copied!
Legacy UI is present in the video, some things might be different

Getting Files via the Public Gateway

An IPFS file can be retrieved with a GET request to a public gateway. The URL for the Moralis gateway is:
  • https://gateway.moralisipfs.com/ipfs/.
For example, https://gateway.moralisipfs.com/ipfs/QmUfpsyqc4hwozotRo4woyi5fJqvfcej5GcFvKiWoY6xr6. A function to fetch a JSON document in IPFS from the browser could be written as the following:
1
async function fetchIPFSDoc(ipfsHash) {
2
const url = `https://gateway.moralisipfs.com/ipfs/${ipfsHash}`;
3
const response = await fetch(url);
4
return await response.json();
5
}
Copied!

Public Gateway Use-Cases

Moralis public IPFS gateway is for:
  1. 1.
    Displaying content on websites that was uploaded via Moralis
Moralis public IPFS gateway is not for:
  1. 1.
    Running scripts downloading content from IPFS. The Moralis public gateway has a JS Challenge blocking scripts for abuse protection. If your use-case requires you to run script downloading a lot of IPFS data we recommend Infura or Pinata.
  2. 2.
    Accessing files on IPFS that weren't uploaded via Moralis.