Skip to main content

📁 File

Any kind of file can be uploaded to Lighthouse. Refer to the code example given below. For developers building on other programming languages use the API directly.

info

Lighthouse currently allows a maximum file size of 24GB to be uploaded in a single request.

Method 1: NodeJS:

import lighthouse from '@lighthouse-web3/sdk'

/**
* This function allows you to upload a file or a folder to Lighthouse.
*
* @param {string} path - The location of your file or folder.
* @param {string} apiKey - Your personal API key for Lighthouse.
* @param {object} options - Optional configuration (cidVersion, onProgress, headers).
* @return {object} - Returns details about the uploaded file.
*/
const uploadResponse = await lighthouse.upload(
'/home/cosmos/Desktop/wow.jpg',
'YOUR_API_KEY_HERE'
)

console.log(uploadResponse)

/*Sample response
{
data: {
Name: 'wow.jpg',
Hash: 'QmUHDKv3NNL1mrg4NTW4WwJqetzwZbGNitdjr2G6Z5Xe6s',
Size: '31735'
}
}
*/

With Options (Progress Tracking & Storage Type):

import lighthouse from '@lighthouse-web3/sdk'

const uploadWithOptions = async () => {
const response = await lighthouse.upload(
'/home/cosmos/Desktop/wow.jpg',
'YOUR_API_KEY_HERE',
{
cidVersion: 1,
onProgress: (progressData) => {
console.log(`Upload Progress: ${progressData.progress}%`)
},
headers: {
storageType: 'lifetime' // Storage plan: "annual" or "lifetime"
}
}
)

console.log('File uploaded:', response.data.Hash)
}
Backward Compatibility

The old positional parameter signature still works: upload(path, apiKey, cidVersion, progressCallback)

Method 2: Browser

import React from "react"
import lighthouse from '@lighthouse-web3/sdk'

function App() {

const uploadFile = async (file) => {
// Push file to lighthouse node
// Both file and folder are supported by upload function
const output = await lighthouse.upload(
file,
"YOUR_API_KEY",
{
onProgress: (progressData) => {
console.log(`Upload Progress: ${progressData.progress}%`)
},
headers: {
storageType: 'lifetime' // Storage plan: "annual" or "lifetime"
}
}
)

console.log('File Status:', output)
/*
output:
data: {
Name: "filename.txt",
Size: 88000,
Hash: "QmWNmn2gr4ZihNPqaC5oTeePsHvFtkWNpjY3cD6Fd5am1w"
}
Note: Hash in response is CID.
*/

console.log('Visit at https://gateway.lighthouse.storage/ipfs/' + output.data.Hash)
}

return (
<div className="App">
<input onChange={e=>uploadFile(e.target.files)} type="file" />
</div>
)
}

export default App