Package Exports
- web4-api-js
- web4-api-js/index.ts
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (web4-api-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
web4-api-js
Simple client library for authentication, view method calls, and contracts calls for apps deployed to web4.
To see it in action or deploy your own profile to web4, try out this example.
Installation
npm install web4-api-js
# or
yarn add web4-api-js
# or
bun add web4-api-jsUsage
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();API Reference
Authentication
login(options?: LoginOptions): void
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)
logout(): void
Logs out the current user and clears web4 session data.
isSignedIn(): boolean
Checks if a user is currently signed in.
getAccountId(): string | undefined
Gets the currently signed in account ID.
getSessionKey(): string | undefined
Gets the current session's private key.
Contract Interaction
view<T>(contractId: string, methodName: string, args?: ViewMethodArgs): Promise<T>
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
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.