Integrating blockchain wallets into decentralized applications (DApps) and decentralized exchanges (DEXs) is a critical step in delivering seamless user experiences. For developers building on the Near blockchain, leveraging an injected provider API like the one offered by OKX Wallet streamlines connectivity, authentication, and transaction handling. This guide dives deep into how to use the OKX injected provider API for Near-based DApps, covering everything from connection setup to contract interaction and event listening.
Whether you're building a DeFi platform, NFT marketplace, or Web3 gaming app, understanding wallet integration is essential. By the end of this article, you'll have a clear roadmap for connecting your DApp to OKX Wallet using JavaScript and the Near protocol.
What Is the Injected Provider API?
The injected provider API is a JavaScript interface that OKX Wallet embeds directly into web pages when users visit DApps. This allows your application to securely interact with the user’s wallet without requiring them to manually configure RPC endpoints or expose private keys.
With this API, your DApp can:
- Request user account access
- Read blockchain data from the connected network
- Prompt users to sign messages and transactions
- Handle wallet connection states and events
All interactions happen client-side, ensuring security and compliance with Web3 best practices.
👉 Discover how easy it is to integrate wallet connectivity into your DApp today.
Accessing the Injected Object
Once a user has OKX Wallet installed and enabled, your DApp can access the injected provider through global window objects. There are two available methods:
window.okxwallet.near— Recommended for clarity and future-proofingwindow.near— A fallback option if multiple wallets are present
Always check for availability before attempting any operations:
if (typeof window.okxwallet !== 'undefined' && window.okxwallet.near) {
const near = window.okxwallet.near;
// Proceed with wallet integration
} else {
console.log("OKX Wallet not detected");
}Using window.okxwallet.near ensures compatibility even in multi-wallet environments.
Connecting to OKX Wallet
Establishing a secure connection with the user's wallet is the first step toward enabling functionality like token swaps, staking, or NFT minting.
requestSignIn()
This method initiates the login flow, prompting the user to authorize your DApp.
Option 1: Only Get the Account ID
For lightweight authentication where full contract permissions aren't needed:
await window.okxwallet.near.requestSignIn({
successUrl: "https://your-dapp.com/dashboard",
failureUrl: "https://your-dapp.com/login"
});After successful sign-in, retrieve the account ID:
const accountId = await window.okxwallet.near.getAccountId();
console.log("Connected account:", accountId);Option 2: Get Access Key with Contract Permissions
To interact with smart contracts, request an access key with specific method permissions:
await window.okxwallet.near.requestSignIn({
contractId: "example-contract.near",
methodNames: ["mint", "transfer"],
successUrl: "https://your-dapp.com/mint",
failureUrl: "https://your-dapp.com"
});This grants limited access to specified view and change methods on the target contract.
signOut()
Disconnect the wallet session:
await window.okxwallet.near.signOut();
console.log("User signed out");Note: The user may also disconnect manually via the wallet extension.
isSignedIn()
Check connection status:
const isLoggedIn = await window.okxwallet.near.isSignedIn();
console.log("User is signed in:", isLoggedIn);getAccountId()
Retrieve the currently connected account identifier:
const accountId = await window.okxwallet.near.getAccountId();This is useful after page reloads or during session restoration.
Signing Messages Securely
Verifying user identity without exposing sensitive data is crucial. Use signMessage to authenticate users off-chain.
signMessage()
Prompt the user to sign a message:
const message = new TextEncoder().encode("Login to MyDApp");
const result = await window.okxwallet.near.signMessage(message);
console.log("Signature:", result.signature);Use cases include:
- Stateless authentication
- Proof-of-ownership checks
- Session initialization
👉 Learn how to implement secure, non-custodial sign-ins in minutes.
Interacting with Smart Contracts
Once connected, your DApp can execute functions on Near smart contracts.
signAndSendTransaction()
Send a single transaction and await its broadcast:
const result = await window.okxwallet.near.signAndSendTransaction({
receiverId: "example-contract.near",
actions: [
{
type: "FunctionCall",
params: {
methodName: "mint",
args: { token_id: "001" },
gas: "30000000000000",
deposit: "1000000000000000000000000"
}
}
]
});
console.log("Transaction hash:", result.transaction.hash);Note: Your DApp must independently monitor the transaction status using the returned txHash.requestSignTransactions()
For batch operations (e.g., multiple NFT mints), request signatures for several transactions at once:
const transactions = [
{
receiverId: "contract-a.near",
actions: [/*...*/]
},
{
receiverId: "contract-b.near",
actions: [/*...*/]
}
];
const results = await window.okxwallet.near.requestSignTransactions(transactions);Important: The wallet signs but does not broadcast these transactions. Your backend or frontend must handle broadcasting logic.
Listening to Wallet Events
Real-time updates enhance UX by reflecting state changes instantly.
signIn
Triggered when the user connects:
window.addEventListener('near#signIn', () => {
console.log("Wallet connected");
updateUIForLoggedInState();
});signOut
Fires when the session ends:
window.addEventListener('near#signOut', () => {
console.log("Wallet disconnected");
resetUI();
});accountChanged
Detect when the user switches accounts:
window.addEventListener('near#accountChanged', (event) => {
const { accountId } = event.detail;
console.log("Switched to account:", accountId);
refreshUserData(accountId);
});These events help maintain sync between your UI and wallet state.
FAQ Section
How do I detect if OKX Wallet is installed?
Check for the presence of window.okxwallet or window.near. If undefined, prompt users to install the extension.
Can I connect to multiple wallets simultaneously?
While multiple wallets may inject objects, only one should be active at a time. Use window.okxwallet.near to prioritize OKX Wallet.
Is user data secure during connection?
Yes. No private keys are exposed. All signing occurs within the wallet’s isolated environment.
What happens if a transaction fails?
Your DApp should use the transaction hash to query blockchain explorers or RPC nodes for detailed error logs.
Does this work on mobile?
Yes, OKX Wallet supports mobile browsers with similar injection behavior via their browser app.
Can I customize the login redirect URLs?
Absolutely. Pass successUrl and failureUrl in requestSignIn() to control post-authentication navigation.
👉 Start integrating powerful wallet features into your Near DApp now.
Core Keywords
- Near wallet integration
- Injected provider API
- DEX API documentation
- OKX Wallet connection
- Web3 JavaScript API
- Sign message Near
- Smart contract interaction
- Decentralized app development
By focusing on these terms naturally throughout the content, this guide aligns with high-intent search queries while maintaining readability and technical accuracy.