nftstorage.link client utilities
Utilities for working with the NFT.Storage IPFS Edge Gateway.
Install
npm install nftstorage.link
Usage
Import the library in your client application like:
import { getGatewayURL } from 'nftstorage.link'
getGatewayURL
Get a gateway URL, given a CID, CID+path, IPFS path or an IPFS gateway URL. If the status of the nftstorage.link
gateway is known to be good (according to the status checker) then return a URL that uses nftstorage.link
, otherwise return an URL that uses dweb.link
(or the optional passed fallback gateway URL). Status result is cached for 60 seconds by default.
Note: the fallback gateway is not guaranteed to be operational and this library makes no attempt to verify this.
getGatewayURL (cid: string|URL): Promise<string>
Examples
const url = await getGatewayURL(
'bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui'
)
console.log(url)
Also works with CID + path:
const url = await getGatewayURL(
'bafyreihwsoxxcxfsisghlc22xzc6datssd7n52wonpdgrhu3lwyqqagzye/metadata.json'
)
console.log(url)
Also works with ipfs://
URL:
const url = await getGatewayURL(
'ipfs://bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui'
)
console.log(url)
Also works with any IPFS gateway URL:
const url = await getGatewayURL(
'https://ipfs.io/ipfs/bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui'
)
console.log(url)
...including subdomain gateway URL:
const url = await getGatewayURL(
'https://bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui.ipfs.dweb.link'
)
console.log(url)
You can also specify a different fallback gateway URL:
const fallbackGatewayURL = 'https://ipfs.io'
const url = await getGatewayURL(
'bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui',
{ fallbackGatewayURL }
)
console.log(url)
React Components
Example of how this function could be used in a React project:
import { useState, useEffect } from 'react'
import { getGatewayURL } from 'nftstorage.link'
function GatewayLink({ cid, title }) {
const [url, setUrl] = useState('')
useEffect(() => {
getGatewayURL(cid).then(setUrl)
}, [cid])
return url ? <a href={url}>{title}</a> : null
}
function GatewayImage({ cid, alt }) {
const [url, setUrl] = useState('')
useEffect(() => {
getGatewayURL(cid).then(setUrl)
}, [cid])
return url ? <img src={url} alt={alt} /> : null
}
See the full example here: https://github.com/nftstorage/react-nftstorage.link-fallback-example
Node.js usage
This library uses the fetch
API. In Node.js there are two options to enable usage:
Assign to global:
import fetch from '@web-std/fetch'
globalThis.fetch = fetch
Pass to GatewayStatusChecker
:
import { getGatewayURL, GatewayStatusChecker } from 'nftstorage.link'
import fetch from '@web-std/fetch'
const statusChecker = new GatewayStatusChecker({ fetch })
const url = await getGatewayURL(
'bafkreiem4twkqzsq2aj4shbycd4yvoj2cx72vezicletlhi7dijjciqpui',
{ statusChecker }
)
Contributing
Feel free to join in. All welcome. Open an issue!
If you're opening a pull request, please see the guidelines in DEVELOPMENT.md on structuring your commit messages so that your PR will be compatible with our release process.
License
Dual-licensed under MIT + Apache 2.0