Main class that interacts with the Nevermined payments API. To get an instance of this class use the getInstance method.

Constructors

Properties

accountAddress?: string
appId?: string
environment: EnvironmentInfo
isBrowserInstance: boolean = true
nvmApiKey?: string
query: AIQueryApi
returnUrl: string
version?: string

Accessors

  • get isLoggedIn(): boolean
  • Property to check if a user logged in.

    Returns boolean

    True if the user is logged in.

    Example

    payments.isLoggedIn
    

Methods

  • Burn credits for a given Payment Plan DID.

    Parameters

    • planDID: string

      The DID (Decentralized Identifier) of the asset.

    • creditsAmount: string

      The amount of NFT (Non-Fungible Token) credits to burn.

    Returns Promise<any>

    A Promise that resolves to the JSON response from the server.

    Remarks

    This method is only can be called by the owner of the Payment Plan.

    Throws

    Error if the server response is not successful.

  • Redirects the user to the subscription checkout page for the specified DID.

    Parameters

    • did: string

      The DID (Decentralized Identifier) of the item to be subscribed to.

    Returns void

    Remarks

    This method is only for browser instances.

  • Initiate the connect flow. The user's browser will be redirected to the Nevermined App login page.

    Returns void

    Remarks

    This is a browser only function.

    Example

    payments.connect()
    
  • It creates a new AI Agent on Nevermined. The agent must be associated to a Payment Plan. Users that are subscribers of a payment plan can access the agent. Depending on the Payment Plan and the configuration of the agent, the usage of the agent/service will consume credits. When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue using the agent.

    Parameters

    • __namedParameters: {
          amountOfCredits?: number;
          apiDescription?: string;
          authType?: "basic" | "none" | "oauth" | "bearer";
          curation?: object;
          description: string;
          endpoints?: Endpoint[];
          implementsQueryProtocol?: boolean;
          integration?: string;
          maxCreditsToCharge?: number;
          minCreditsToCharge?: number;
          name: string;
          openApiUrl?: string;
          openEndpoints?: string[];
          password?: string;
          planDID: string;
          sampleLink?: string;
          serviceChargeType: "fixed" | "dynamic";
          tags?: string[];
          token?: string;
          username?: string;
          usesAIHub?: boolean;
      }
      • Optional amountOfCredits?: number
      • Optional apiDescription?: string
      • Optional authType?: "basic" | "none" | "oauth" | "bearer"
      • Optional curation?: object
      • description: string
      • Optional endpoints?: Endpoint[]
      • Optional implementsQueryProtocol?: boolean
      • Optional integration?: string
      • Optional maxCreditsToCharge?: number
      • Optional minCreditsToCharge?: number
      • name: string
      • Optional openApiUrl?: string
      • Optional openEndpoints?: string[]
      • Optional password?: string
      • planDID: string
      • Optional sampleLink?: string
      • serviceChargeType: "fixed" | "dynamic"
      • Optional tags?: string[]
      • Optional token?: string
      • Optional username?: string
      • Optional usesAIHub?: boolean

    Returns Promise<{
        did: string;
    }>

    A promise that resolves to the created agent DID.

    Remarks

    This method is oriented to AI Builders

    See

    Example

    const agentDID = await paymentsBuilder.createService({
    planDID,
    name: 'E2E Payments Agent',
    description: 'description',
    serviceType: 'agent',
    serviceChargeType: 'fixed',
    authType: 'bearer',
    token: 'changeme',
    amountOfCredits: 1,
    endpoints: agentEndpoints,
    openEndpoints: ['https://example.com/api/v1/rest/docs-json']
    })
  • It allows to an AI Builder to create a Payment Plan on Nevermined based on Credits. A Nevermined Credits Plan limits the access by the access/usage of the Plan. With them, AI Builders control the number of requests that can be made to an agent or service. Every time a user accesses any resouce associated to the Payment Plan, the usage consumes from a capped amount of credits. When the user consumes all the credits, the plan automatically expires and the user needs to top up to continue using the service.

    Parameters

    • __namedParameters: {
          amountOfCredits: number;
          description: string;
          name: string;
          price: bigint;
          tags?: string[];
          tokenAddress: string;
      }
      • amountOfCredits: number
      • description: string
      • name: string
      • price: bigint
      • Optional tags?: string[]
      • tokenAddress: string

    Returns Promise<{
        did: string;
    }>

    The unique identifier of the plan (Plan DID) of the newly created plan.

    Remarks

    This method is oriented to AI Builders

    See

    https://docs.nevermined.app/docs/tutorials/builders/create-plan

    Example

     const { did } = await payments.createCreditsPlan({
    name: "My AI Payments Plan",
    description: "AI stuff",
    price: 10000000n,
    tokenAddress: "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",
    amountOfCredits: 30,
    tags: ["test"]
    })
  • It creates a new asset with file associated to it. The file asset must be associated to a Payment Plan. Users that are subscribers of a payment plan can download the files attached to it. Depending on the Payment Plan and the configuration of the file asset, the download will consume credits. When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue downloading the files.

    Parameters

    • __namedParameters: {
          amountOfCredits?: number;
          assetType: "file" | "dataset" | "other" | "algorithm" | "model";
          curation?: object;
          dataSchema?: string;
          description: string;
          files: object[];
          filesFormat?: string;
          fineTunable?: boolean;
          framework?: string;
          maxCreditsToCharge?: number;
          minCreditsToCharge?: number;
          name: string;
          planDID: string;
          programmingLanguage?: string;
          sampleCode?: string;
          tags?: string[];
          task?: string;
          trainingDetails?: string;
          usageExample?: string;
          variations?: string;
      }
      • Optional amountOfCredits?: number
      • assetType: "file" | "dataset" | "other" | "algorithm" | "model"
      • Optional curation?: object
      • Optional dataSchema?: string
      • description: string
      • files: object[]
      • Optional filesFormat?: string
      • Optional fineTunable?: boolean
      • Optional framework?: string
      • Optional maxCreditsToCharge?: number
      • Optional minCreditsToCharge?: number
      • name: string
      • planDID: string
      • Optional programmingLanguage?: string
      • Optional sampleCode?: string
      • Optional tags?: string[]
      • Optional task?: string
      • Optional trainingDetails?: string
      • Optional usageExample?: string
      • Optional variations?: string

    Returns Promise<{
        did: string;
    }>

    The promise that resolves to the created file's DID.

  • It creates a new AI Agent or Service on Nevermined. The agent/service must be associated to a Payment Plan. Users that are subscribers of a payment plan can access the agent/service. Depending on the Payment Plan and the configuration of the agent/service, the usage of the agent/service will consume credits. When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue using the agent/service.

    Parameters

    • __namedParameters: {
          amountOfCredits?: number;
          apiDescription?: string;
          authType?: "basic" | "none" | "oauth" | "bearer";
          curation?: object;
          description: string;
          endpoints?: Endpoint[];
          implementsQueryProtocol?: boolean;
          integration?: string;
          maxCreditsToCharge?: number;
          minCreditsToCharge?: number;
          name: string;
          openApiUrl?: string;
          openEndpoints?: string[];
          password?: string;
          planDID: string;
          sampleLink?: string;
          serviceChargeType: "fixed" | "dynamic";
          serviceType: "agent" | "service" | "assistant";
          tags?: string[];
          token?: string;
          username?: string;
          usesAIHub?: boolean;
      }
      • Optional amountOfCredits?: number
      • Optional apiDescription?: string
      • Optional authType?: "basic" | "none" | "oauth" | "bearer"
      • Optional curation?: object
      • description: string
      • Optional endpoints?: Endpoint[]
      • Optional implementsQueryProtocol?: boolean
      • Optional integration?: string
      • Optional maxCreditsToCharge?: number
      • Optional minCreditsToCharge?: number
      • name: string
      • Optional openApiUrl?: string
      • Optional openEndpoints?: string[]
      • Optional password?: string
      • planDID: string
      • Optional sampleLink?: string
      • serviceChargeType: "fixed" | "dynamic"
      • serviceType: "agent" | "service" | "assistant"
      • Optional tags?: string[]
      • Optional token?: string
      • Optional username?: string
      • Optional usesAIHub?: boolean

    Returns Promise<{
        did: string;
    }>

    A promise that resolves to the created agent DID.

    Remarks

    This method is oriented to AI Builders

    See

    Example

    const agentEndpoints: Endpoint[] = [
    { 'POST': `https://example.com/api/v1/agents/(.*)/tasks` },
    { 'GET': `https://example.com/api/v1/agents/(.*)/tasks/(.*)` }
    ]
    const agentDID = await paymentsBuilder.createService({
    planDID,
    name: 'E2E Payments Agent',
    description: 'description',
    serviceType: 'agent',
    serviceChargeType: 'fixed',
    authType: 'bearer',
    token: 'changeme',
    amountOfCredits: 1,
    endpoints: agentEndpoints,
    openEndpoints: ['https://example.com/api/v1/rest/docs-json']
    })
  • It allows to an AI Builder to create a Payment Plan on Nevermined based on Time. A Nevermined Time Plan limits the access by the a specific amount of time. With them, AI Builders can specify the duration of the Payment Plan (1 month, 1 year, etc.). When the time period is over, the plan automatically expires and the user needs to renew it.

    Parameters

    • __namedParameters: {
          description: string;
          duration?: number;
          name: string;
          price: bigint;
          tags?: string[];
          tokenAddress: string;
      }
      • description: string
      • Optional duration?: number
      • name: string
      • price: bigint
      • Optional tags?: string[]
      • tokenAddress: string

    Returns Promise<{
        did: string;
    }>

    The unique identifier of the plan (Plan DID) of the newly created plan.

    Remarks

    This method is oriented to AI Builders

    See

    https://docs.nevermined.app/docs/tutorials/builders/create-plan

    Example

     const { did } = await payments.createTimePlan({
    name: "My 1 Month Plan",
    description: "test",
    price: 10000000n,
    tokenAddress: "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",
    duration: 30,
    tags: ["test"]
    })
  • Downloads files for a given DID asset.

    Parameters

    • fileDid: string

    Returns Promise<string>

    A promise that resolves to the JSON response from the server.

  • Redirects the user to the file details for the specified DID (Decentralized Identifier).

    Parameters

    • did: string

      The DID of the file.

    Returns void

    Remarks

    This method is only for browser instances.

  • Get array of files DIDs associated with a payment plan.

    Parameters

    • planDID: string

      The DID of the Payment Plan.

    Returns Promise<any>

    A promise that resolves to array of files DIDs.

  • Get array of services/agent DIDs associated with a payment plan.

    Parameters

    • planDID: string

      The DID of the Payment Plan.

    Returns Promise<any>

    A promise that resolves to the array of services/agents DIDs.

  • Get the balance of an account for a Payment Plan.

    Parameters

    • planDID: string

      The Payment Plan DID of the service to be published.

    • Optional accountAddress: string

      The address of the account to get the balance.

    Returns Promise<{
        balance: bigint;
        isOwner: boolean;
        isSubscriptor: boolean;
        subscriptionType: string;
    }>

    A promise that resolves to the balance result.

  • Redirects the user to the subscription details for a given DID.

    Parameters

    • planDID: string

      The DID (Decentralized Identifier) of the plan.

    Returns void

    Remarks

    This method is only for browser instances.

  • Get the required configuration for accessing a remote service agent. This configuration includes:

    • The JWT access token
    • The Proxy url that can be used to query the agent/service.

    Parameters

    • did: string

      The DID of the agent/service.

    Returns Promise<{
        accessToken: string;
        neverminedProxyUri: string;
    }>

    A promise that resolves to the service token.

    Example

    const accessConfig = await payments.getServiceAccessConfig(agentDID)
    console.log(`Agent JWT Token: ${accessConfig.accessToken}`)
    console.log(`Agent Proxy URL: ${accessConfig.neverminedProxyUri}`)
  • Redirects the user to the service details for a given DID.

    Parameters

    • did: string

      The DID (Decentralized Identifier) of the service.

    Returns void

    Remarks

    This method is only for browser instances.

  • Method to initialize the class once the user has been logged in. This method should be called has soon as the user has been redirected back to the app (returnUrl).

    Returns void

    Remarks

    This is a browser only function.

    Example

    payments.init()
    

    Example: Using react

    You may want to use useEffect on the route that matches the passed returnUrl

    useEffect(() => {
    payments.init()
    })
  • Initializes the AI Query Protocol API.

    Returns void

  • Logout the user by removing the nvm api key.

    Returns void

    Remarks

    This is a browser only function.

    Example

    payments.logout()
    
  • Mint credits for a given Payment Plan DID and transfer them to a receiver.

    Parameters

    • planDID: string

      The DID (Decentralized Identifier) of the asset.

    • creditsAmount: string

      The amount of NFT (Non-Fungible Token) credits to mint.

    • receiver: string

      The address of the receiver where the credits will be transferred.

    Returns Promise<any>

    A Promise that resolves to the JSON response from the server.

    Remarks

    This method is only can be called by the owner of the Payment Plan.

    Throws

    Error if the server response is not successful.

  • Orders a Payment Plan. The user needs to have enough balance in the token selected by the owner of the Payment Plan.

    Parameters

    • planDID: string

      The Payment Plan DID of the service to be published.

    • Optional agreementId: string

      The unique identifier of the purchase transaction (aka agreement ID). When this parameter is given, it assumes there is a previous payment step and will request the payment plan.

    Returns Promise<{
        agreementId: string;
        success: boolean;
    }>

    A promise that resolves to the agreement ID and a boolean indicating if the operation was successful.

    Remarks

    The payment is done using Crypto. Payments using Fiat can be done via the Nevermined App.

  • It parses the NVM API Key to get the account address.

    Returns void

  • The options get's an instance of the payments class to be used in the browser.

    Parameters

    Returns Payments

    An instance of Payments

    Remarks

    This is a browser only function.

    Example

    const payments = Payments.getBrowserInstance({
    returnUrl: 'https://mysite.example',
    environment: 'testing',
    appId: 'my-app-id',
    version: '1.0.0'
    })
  • The options get's an instance of the payments class.

    Parameters

    Returns Payments

    An instance of Payments

    Example

    const payments = Payments.getInstance({
    nvmApiKey: 'kjdfaofakdoasdkoas',
    environment: 'testing'
    })