@nevermined-io/payments
TypeScript icon, indicating that this package has built-in type declarations

0.7.2 • Public • Published

banner

Typescript SDK to interact with the Nevermined Payments Protocol

Typescript SDK to interact with the Nevermined Payments Protocol nevermined.io

Quickstart

# yarn
yarn add @nevermined-io/payments

# npm

npm install @nevermined-io/payments

Initialize the payments library

This is a browser only method. Here we have an example using react. For a full example please refer to payments-nextjs-example

import { useEffect } from "react";
import { Payments } from "@nevermined-io/payments";

export default function Home() {
  const payments = new Payments({
    returnUrl: "http://localhost:8080",
    environment: "staging",
  });

  const onLogin = () => {
    payments.connect();
  };

  useEffect(() => {
    payments.init();
  }, []);

  return (
    <main>
      <div>
        <button onClick={onLogin}>Login</button>
      </div>
    </main>
  );
}

The init() method should be called immediately after the app returns the user to returnUrl.

Create a Payments Plan

Once the app is initialized we can create a payment plan:

const planDID = await payments.createCreditsPlan({
    name: "My AI Payments Plan",
    description: "AI stuff",
    price: 10000000n,
    tokenAddress: "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",
    amountOfCredits: 30,
    tags: ["test"]
  })

Create an AI Agent/Service

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']
})

Package Sidebar

Install

npm i @nevermined-io/payments

Weekly Downloads

185

Version

0.7.2

License

Apache-2.0

Unpacked Size

183 kB

Total Files

39

Last publish

Collaborators

  • aitorargomaniz