Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.near.org/llms.txt

Use this file to discover all available pages before exploring further.

Privy is a third-party service that allows users to login using their email or social accounts (Google, Facebook, Twitter, etc). Upon login, a NEAR wallet is created for the user, which they can fund and use to interact with your dApp. Preview See full working example here
Web3Auth For an alternative social login method you can check Web3Auth. We have a functional Web3Auth Integration Example to show how to integrate Web3Auth into your web app.

Installation

npm install @privy-io/react-auth @near-js/accounts @near-js/providers @near-js/transactions @near-js/tokens @noble/hashes

Setting Up the Providers

Wrap your application with PrivyProvider and a custom NEARxPrivy context that bridges Privy’s authentication with a NEAR account:
  1. Sign up at privy.io and create a new app.
  2. Copy the App ID from the dashboard and set it as your appId prop.
  3. Configure allowed login methods and any other settings in the Privy dashboard.

NEAR Context

The NEARxPrivy provider listens to Privy’s authentication state. Once the user is logged in, it creates a NEAR wallet (if one doesn’t exist yet) and exposes a nearAccount instance through context:
ValueTypeDescription
walletIdstringThe user’s NEAR account address
nearAccountAccountA @near-js/accounts Account ready to sign transactions
providerJsonRpcProviderA read-only RPC provider to query the network

Custom Signer

Privy signs transactions through its embedded wallet via signRawHash. The privySigner class adapts that API to the interface expected by @near-js/accounts:

Signing In / Out

Use useLogin and useLogout from Privy to open the login modal and sign the user out:

Calling Read-only Methods

Use the provider from context to call view methods — no login required:
const greeting = await provider.callFunction(CONTRACT, 'get_greeting', {})

Calling Change Methods

Use the nearAccount from context to call change methods. The transaction is signed silently by Privy’s embedded wallet, providing a Web2-like UX:
await nearAccount.callFunction({
  contractId: CONTRACT,
  methodName: 'set_greeting',
  args: { greeting: newGreeting },
})

Get Balance

import { NEAR } from '@near-js/tokens'

const balance = await nearAccount.getBalance()
const formatted = Number(NEAR.toDecimal(balance)).toFixed(2)
console.log(`Balance: ${formatted} Ⓝ`)

Full Example