Options
All
  • Public
  • Public/Protected
  • All
Menu

Class NFTStorage

implements

{Service}

Hierarchy

  • NFTStorage

Index

Constructors

  • Constructs a client bound to the given options.token and options.endpoint.

    example
    import { NFTStorage, File, Blob } from "nft.storage"
    const client = new NFTStorage({ token: API_TOKEN })

    const cid = await client.storeBlob(new Blob(['hello world']))

    Optionally you could pass an alternative API endpoint (e.g. for testing)

    example
    import { NFTStorage } from "nft.storage"
    const client = new NFTStorage({
    token: API_TOKEN
    endpoint: new URL('http://localhost:8080/')
    })

    Parameters

    • options: { did?: string; endpoint?: URL; rateLimiter?: RateLimiter; token: string }
      • Optional did?: string
      • Optional endpoint?: URL
      • Optional rateLimiter?: RateLimiter
      • token: string

    Returns NFTStorage

Properties

did: undefined | string
readonly
endpoint: URL

Service API endpoint URL.

readonly
rateLimiter: RateLimiter
readonly
token: string

Authorization token.

readonly

Methods

  • Check if a CID of an NFT is being stored by nft.storage. Throws if the NFT was not found.

    example
    const status = await client.check('zdj7Wn9FQAURCP6MbwcWuzi7u65kAsXCdjNTkhbJcoaXBusq9')
    

    Parameters

    Returns Promise<CheckResult>

  • Removes stored content by its CID from the service.

    Please note that even if content is removed from the service other nodes that have replicated it might still continue providing it.

    example
    await client.delete('zdj7Wn9FQAURCP6MbwcWuzi7u65kAsXCdjNTkhbJcoaXBusq9')
    

    Parameters

    Returns Promise<void>

  • Returns current status of the stored NFT by its CID. Note the NFT must have previously been stored by this account.

    example
    const status = await client.status('zdj7Wn9FQAURCP6MbwcWuzi7u65kAsXCdjNTkhbJcoaXBusq9')
    

    Parameters

    Returns Promise<StatusResult>

  • Stores the given token and all resources it references (in the form of a File or a Blob) along with a metadata JSON as specificed in ERC-1155. The token.image must be either a File or a Blob instance, which will be stored and the corresponding content address URL will be saved in the metadata JSON file under image field.

    If token.properties contains properties with File or Blob values, those also get stored and their URLs will be saved in the metadata JSON file in their place.

    Note: URLs for File objects will retain file names e.g. in case of new File([bytes], 'cat.png', { type: 'image/png' }) will be transformed into a URL that looks like ipfs://bafy...hash/image/cat.png. For Blob objects, the URL will not have a file name name or mime type, instead it will be transformed into a URL that looks like ipfs://bafy...hash/image/blob.

    example
    const metadata = await client.store({
    name: 'nft.storage store test',
    description: 'Test ERC-1155 compatible metadata.',
    image: new File(['<DATA>'], 'pinpie.jpg', { type: 'image/jpg' }),
    properties: {
    custom: 'Custom data can appear here, files are auto uploaded.',
    file: new File(['<DATA>'], 'README.md', { type: 'text/plain' }),
    }
    })

    console.log('IPFS URL for the metadata:', metadata.url)
    console.log('metadata.json contents:\n', metadata.data)
    console.log('metadata.json with IPFS gateway URLs:\n', metadata.embed())

    Type Parameters

    Parameters

    Returns Promise<Token<T>>

  • Stores a single file and returns the corresponding Content Identifier (CID). Takes a Blob or a File. Note that no file name or file metadata is retained.

    example
    const content = new Blob(['hello world'])
    const cid = await client.storeBlob(content)
    cid //> 'zdj7Wn9FQAURCP6MbwcWuzi7u65kAsXCdjNTkhbJcoaXBusq9'

    Parameters

    Returns Promise<CIDString>

  • Stores files encoded as a single Content Addressed Archive (CAR).

    Takes a Blob or a File.

    Returns the corresponding Content Identifier (CID).

    See the ipfs-car docs for more details on packing a CAR file.

    example
    import { pack } from 'ipfs-car/pack'
    import { CarReader } from '@ipld/car'
    const { out, root } = await pack({
    input: fs.createReadStream('pinpie.pdf')
    })
    const expectedCid = root.toString()
    const carReader = await CarReader.fromIterable(out)
    const cid = await storage.storeCar(carReader)
    console.assert(cid === expectedCid)
    example
    import { packToBlob } from 'ipfs-car/pack/blob'
    const data = 'Hello world'
    const { root, car } = await packToBlob({ input: [new TextEncoder().encode(data)] })
    const expectedCid = root.toString()
    const cid = await client.storeCar(car)
    console.assert(cid === expectedCid)

    Parameters

    Returns Promise<CIDString>

  • Stores a directory of files and returns a CID for the directory.

    example
    const cid = await client.storeDirectory([
    new File(['hello world'], 'hello.txt'),
    new File([JSON.stringify({'from': 'incognito'}, null, 2)], 'metadata.json')
    ])
    cid //>

    Argument can be a FileList instance as well, in which case directory structure will be retained.

    Parameters

    Returns Promise<CIDString>

  • Removes stored content by its CID from this account. Please note that even if content is removed from the service other nodes that have replicated it might still continue providing it.

    Parameters

    Returns Promise<void>

  • encodeBlob(blob: Blob, [options]?: undefined | { blockstore: undefined | Blockstore }): Promise<{ car: CarReader; cid: CID }>
  • Encodes a single file to a CAR file and also returns its root CID.

    example
    const content = new Blob(['hello world'])
    const { cid, car } = await NFTStorage.encodeBlob(content)

    // Root CID of the file
    console.log(cid.toString())

    // Now store the CAR file on NFT.Storage
    await client.storeCar(car)

    Parameters

    • blob: Blob
    • [options]: undefined | { blockstore: undefined | Blockstore } = {}

    Returns Promise<{ car: CarReader; cid: CID }>

    }

  • encodeDirectory(files: FilesSource, [options]?: undefined | { blockstore: undefined | Blockstore }): Promise<{ car: CarReader; cid: CID }>
  • Encodes a directory of files to a CAR file and also returns the root CID. Provided files MUST be within the same directory, otherwise error is raised e.g. foo/bar.png, foo/bla/baz.json is ok but foo/bar.png, bla/baz.json is not.

    example
    const { cid, car } = await NFTStorage.encodeDirectory([
    new File(['hello world'], 'hello.txt'),
    new File([JSON.stringify({'from': 'incognito'}, null, 2)], 'metadata.json')
    ])

    // Root CID of the directory
    console.log(cid.toString())

    // Now store the CAR file on NFT.Storage
    await client.storeCar(car)

    Parameters

    • files: FilesSource
    • [options]: undefined | { blockstore: undefined | Blockstore } = {}

    Returns Promise<{ car: CarReader; cid: CID }>

    }

  • encodeNFT<T>(input: T): Promise<{ car: CarReader; cid: CID; token: Token<T> }>
  • Encodes the given token and all resources it references (in the form of a File or a Blob) along with a metadata JSON as specificed in ERC-1155 to a CAR file. The token.image must be either a File or a Blob instance, which will be stored and the corresponding content address URL will be saved in the metadata JSON file under image field.

    If token.properties contains properties with File or Blob values, those also get stored and their URLs will be saved in the metadata JSON file in their place.

    Note: URLs for File objects will retain file names e.g. in case of new File([bytes], 'cat.png', { type: 'image/png' }) will be transformed into a URL that looks like ipfs://bafy...hash/image/cat.png. For Blob objects, the URL will not have a file name name or mime type, instead it will be transformed into a URL that looks like ipfs://bafy...hash/image/blob.

    example
    const { token, car } = await NFTStorage.encodeNFT({
    name: 'nft.storage store test',
    description: 'Test ERC-1155 compatible metadata.',
    image: new File(['<DATA>'], 'pinpie.jpg', { type: 'image/jpg' }),
    properties: {
    custom: 'Custom data can appear here, files are auto uploaded.',
    file: new File(['<DATA>'], 'README.md', { type: 'text/plain' }),
    }
    })

    console.log('IPFS URL for the metadata:', token.url)
    console.log('metadata.json contents:\n', token.data)
    console.log('metadata.json with IPFS gateway URLs:\n', token.embed())

    // Now store the CAR file on NFT.Storage
    await client.storeCar(car)

    Type Parameters

    Parameters

    • input: T

    Returns Promise<{ car: CarReader; cid: CID; token: Token<T> }>

    }

  • Stores the given token and all resources it references (in the form of a File or a Blob) along with a metadata JSON as specificed in ERC-1155. The token.image must be either a File or a Blob instance, which will be stored and the corresponding content address URL will be saved in the metadata JSON file under image field.

    If token.properties contains properties with File or Blob values, those also get stored and their URLs will be saved in the metadata JSON file in their place.

    Note: URLs for File objects will retain file names e.g. in case of new File([bytes], 'cat.png', { type: 'image/png' }) will be transformed into a URL that looks like ipfs://bafy...hash/image/cat.png. For Blob objects, the URL will not have a file name name or mime type, instead it will be transformed into a URL that looks like ipfs://bafy...hash/image/blob.

    Type Parameters

    Parameters

    Returns Promise<Token<T>>

  • Stores a directory of files and returns a CID. Provided files MUST be within the same directory, otherwise error is raised e.g. foo/bar.png, foo/bla/baz.json is ok but foo/bar.png, bla/baz.json is not.

    Parameters

    Returns Promise<CIDString>

Generated using TypeDoc