IPFS
Moralis Has Native Support for IPFS!

Configuring IPFS

⚠️ This is no longer required. IPFS is enabled by default. There used to be a tab for this in "View Details" on the server instance but this has been removed.

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", {base64 : btoa(JSON.stringify(object))});
5
await file.saveIPFS();
Copied!
By uploading base64, you could also upload other base64 encoded files such as images.
1
const image = "data:image/png;base64,iVBORw0KGgoAAA...."
2
const file = new Moralis.File("image.png", {base64 : image });
3
await file.saveIPFS();
Copied!

Getting Files

An IPFS file can be retrieved with a GET request to a public gateway. The URL for the Moralis gateway is:
  • https://ipfs.moralis.io:2053/ipfs/.
For example, https://ipfs.moralis.io:2053/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://ipfs.moralis.io:2053/ipfs/${ipfsHash}`;
3
const response = await fetch(url);
4
return await response.json();
5
}
Copied!
Last modified 2mo ago