Options
All
  • Public
  • Public/Protected
  • All
Menu

Class NFTStorage

implements

API.Service

Hierarchy

  • NFTStorage

Index

Constructors

constructor

  • new NFTStorage(__namedParameters: { endpoint?: URL; token: string }): NFTStorage
  • 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

    • __namedParameters: { endpoint?: URL; token: string }
      • Optional endpoint?: URL
      • token: string

    Returns NFTStorage

Properties

endpoint

endpoint: URL

token

token: string

Methods

check

  • 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('Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD')
    

    Parameters

    • cid: string

    Returns Promise<CheckResult>

delete

  • delete(cid: string): Promise<void>
  • 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('Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD')
    

    Parameters

    • cid: string

    Returns Promise<void>

status

  • 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('Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD')
    

    Parameters

    • cid: string

    Returns Promise<StatusResult>

store

  • store<T>(token: T): Promise<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.

    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

    • token: T

    Returns Promise<Token<T>>

storeBlob

  • 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 //> 'Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD'
    

    Parameters

    • blob: Blob

    Returns Promise<CIDString>

storeCar

  • 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 { packToBlob } from 'ipfs-car'
    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

    • blob: Blob

    Returns Promise<CIDString>

storeDirectory

  • storeDirectory(files: Iterable<File>): 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

    • files: Iterable<File>

    Returns Promise<CIDString>

Static check

Static delete

  • delete(__namedParameters: Service, cid: string): Promise<void>

Static status

Static store

  • store<T>(__namedParameters: Service, metadata: T): Promise<Token<T>>

Static storeBlob

Static storeCar

Static storeDirectory

  • storeDirectory(__namedParameters: Service, files: Iterable<File>): Promise<CIDString>

Generated using TypeDoc