Provider API for NEAR: Connect Your DApp to Wallet Extension

·

Integrating a crypto wallet into your decentralized application (DApp) is a critical step in delivering a seamless blockchain experience. For developers building on the NEAR protocol, the injected provider API offered by OKX Wallet enables secure, efficient, and user-friendly wallet connectivity. This guide walks you through everything you need to know about connecting your DApp to OKX Wallet using the NEAR-injected JavaScript API — from initialization to transaction handling and event listening.

Whether you're building a decentralized exchange (DEX), NFT marketplace, or DeFi protocol, understanding how to properly integrate wallet functionality ensures better security, improved UX, and broader user adoption.

👉 Discover how easy it is to integrate wallet connectivity into your NEAR-based DApp today.

What Is the Injected Provider API?

The OKX injected provider API is a JavaScript interface automatically embedded into web pages when accessed by users with the OKX Wallet extension enabled. It acts as a bridge between your DApp and the user's wallet, allowing your application to:

This injection happens client-side, meaning no sensitive private keys are ever exposed to your app. Instead, all signing operations are handled within the secure environment of the wallet extension.

By leveraging this API, developers can create non-custodial applications where users retain full control over their assets — a cornerstone principle of Web3.

Accessing the Injected Object

Your DApp can interact with the OKX Wallet via the global window object. There are two ways to access the NEAR provider:

Using window.okxwallet.near is preferred because it explicitly targets OKX Wallet, reducing ambiguity in multi-wallet environments.

if (window.okxwallet && window.okxwallet.near) {
  const near = window.okxwallet.near;
  // Proceed with wallet integration
} else {
  console.log("OKX Wallet not detected");
}

Always check for the presence of the provider before attempting any operations to ensure a smooth fallback experience.

Connecting to OKX Wallet

To initiate a connection, use the requestSignIn() method. This prompts the user to authenticate and grant your DApp access to their account.

Request Sign-In with Account ID Only

To simply retrieve the user’s NEAR account ID:

await window.okxwallet.near.requestSignIn({
  successUrl: "https://yourdapp.com/dashboard",
  failureUrl: "https://yourdapp.com/welcome"
});

After successful authentication, you can retrieve the account ID using getAccountId().

Request Sign-In with Contract Access (Full Permissions)

If your DApp interacts with smart contracts, request access keys with specific permissions:

await window.okxwallet.near.requestSignIn({
  contractId: "example-contract.near",
  methodNames: ["mint", "transfer"],
  successUrl: "https://yourdapp.com/mint",
  failureUrl: "https://yourdapp.com/error"
});

This grants your DApp limited access to call specified methods on the given contract. The wallet generates and stores an access key securely.

Return Value Example:

{
  "accountId": "user123.near",
  "publicKey": "ed25519:...",
  "accessToken": "..."
}

Check Connection Status

Use these methods to manage session state:

These are essential for maintaining UI consistency and restoring sessions after page reloads.

Disconnecting from Wallet

Allow users to disconnect at any time using:

await window.okxwallet.near.signOut();

Note that disconnection can also be initiated from within the wallet itself.

Signing Messages

Securely verify user identity or sign off-chain data using signMessage():

const message = new TextEncoder().encode("Hello NEAR");
const signed = await window.okxwallet.near.signMessage(message);

This returns a signature along with public key information, useful for authentication flows without gas costs.

👉 Learn how message signing enhances security in your NEAR DApp.

Interacting with Smart Contracts

Once connected, your DApp can interact with NEAR smart contracts through transaction requests.

Sign and Send a Single Transaction

Use signAndSendTransaction() for simple actions like token transfers or function calls:

const outcome = await window.okxwallet.near.signAndSendTransaction({
  receiverId: "token.near",
  actions: [
    {
      type: "FunctionCall",
      params: {
        methodName: "ft_transfer",
        args: { receiver_id: "user123.near", amount: "1000" },
        gas: "30000000000000",
        deposit: "1"
      }
    }
  ]
});

Note: You must monitor transaction status using the returned txHash to confirm blockchain confirmation.

Batch Sign Multiple Transactions

For complex workflows, use requestSignTransactions() to request signatures for multiple transactions at once:

await window.okxwallet.near.requestSignTransactions({
  transactions: [
    {
      receiverId: "contract1.near",
      actions: [/*...*/]
    },
    {
      receiverId: "contract2.near",
      actions: [/*...*/]
    }
  ]
});

The wallet will sign all transactions but will not broadcast them. Your DApp must handle broadcasting logic separately.

Listening to Wallet Events

Real-time updates improve user experience. Subscribe to key events emitted by OKX Wallet:

signIn

Fired when the user successfully connects their wallet.

window.addEventListener("near#signIn", () => {
  console.log("User signed in");
  updateUI();
});

signOut

Triggered when the user disconnects.

window.addEventListener("near#signOut", () => {
  console.log("User signed out");
  resetUI();
});

accountChanged

Emitted when the active account changes (e.g., switching profiles).

window.addEventListener("near#accountChanged", (event) => {
  const { accountId } = event.detail;
  console.log("Account switched to:", accountId);
  refreshUserData(accountId);
});

These events help keep your DApp synchronized with the user’s wallet state across tabs and sessions.

Core Keywords for SEO Optimization

To align with search intent and improve visibility, this guide naturally incorporates the following core keywords:

These terms reflect common queries from developers seeking documentation on integrating NEAR-compatible wallets into their decentralized applications.

Frequently Asked Questions

How do I detect if OKX Wallet is installed?

Check for the existence of window.okxwallet.near. If undefined, prompt the user to install the extension.

Can I connect without redirecting the user?

Yes. While requestSignIn() traditionally uses redirects (successUrl/failureUrl), modern implementations support modal-based authentication that keeps users on-page.

Is the injected API secure?

Yes. The API never exposes private keys. All signing occurs inside the isolated wallet environment, protecting user assets.

What happens if a transaction fails?

Your DApp should use the transaction hash to query network status and display appropriate feedback. Failed transactions are recorded on-chain with error details.

Can I use this API on mobile?

Yes. OKX Wallet supports both desktop extensions and mobile apps, with consistent API behavior across platforms.

Does this work with other NEAR wallets?

While similar APIs exist (e.g., Nightly Wallet), using window.okxwallet.near ensures compatibility specifically with OKX Wallet features and event naming.

👉 Start integrating secure, high-performance wallet connectivity now.