Simple client library for authentication, view method calls, and contracts calls for apps deployed to web4.
- /web4/login →
login()
- /web4/logout →
logout()
GET
/web4/contract/{contract_id}/{method_name} →await view()
POST
/web4/contract/{contract_id}/{method_name} →await call()
To see it in action or deploy your own profile to web4, try out this example.
npm install web4-api-js
# or
yarn add web4-api-js
# or
bun add web4-api-js
import { login, logout, isSignedIn, getAccountId, view, call } from 'web4-api-js';
// Authentication
if (!isSignedIn()) {
login({
contractId: 'example.near',
callbackPath: '/dashboard'
});
}
const accountId = getAccountId();
console.log('Logged in as:', accountId);
// View method (read-only)
const balance = await view(
'token.near',
'ft_balance_of',
{ account_id: accountId }
);
// Call method
await call(
'token.near',
'ft_transfer',
{
receiver_id: 'bob.near',
amount: '1000000000000000000000000'
},
{
deposit: '1', // in yoctoNEAR
gas: '100000000000000', // 100 TGas
callbackUrl: '/transfer/success'
}
);
// Logout
logout();
Initiates the web4 login process by redirecting to global login page
options.contractId
: Contract requiring access (optional)options.callbackPath
: Path to return to after login (optional)
Logs out the current user and clears web4 session data.
Checks if a user is currently signed in.
Gets the currently signed in account ID.
Gets the current session's private key.
Calls a view method on a web4 contract.
contractId
: The contract to callmethodName
: The view method to callargs
: Arguments to pass to the method (optional)- Returns: Promise resolving to the method's return value
call<T>(contractId: string, methodName: string, args: ContractCallArgs, options?: ContractCallOptions): Promise<T>
Calls a method on a web4 contract that can modify state.
contractId
: The contract to callmethodName
: The method to callargs
: Arguments to pass to the methodoptions
: Optional call configurationgas
: Gas limit for the transactiondeposit
: Amount of NEAR to attach to the callcallbackUrl
: URL to return to after transaction completion
- Returns: Promise resolving to the execution outcome or redirects for signing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.