home / skills / near / agent-skills / near-dapp

near-dapp skill

/skills/near-dapp

This skill guides you to create NEAR dApps, integrate wallet connections, and build frontend interactions for NEAR smart contracts.

npx playbooks add skill near/agent-skills --skill near-dapp

Review the files below or copy the command above to add this skill to your agents.

Files (4)
SKILL.md
2.7 KB
---
name: near-dapp
description: >
  Build NEAR Protocol dApps. Use for: (1) creating new NEAR dApps with
  `create-near-app` (Vite+React, Next.js), (2) adding NEAR wallet connection
  to existing apps with `@hot-labs/near-connect` and `near-connect-hooks`,
  (3) building frontend UI for NEAR smart contracts, (4) integrating wallet
  sign-in/sign-out, contract calls, and transaction signing into web
  applications.
---

# NEAR dApp

## Decision Router

Determine which path to follow:

### Path A: New Project

User wants to create a new NEAR dApp from scratch.

1. Read [references/create-near-app.md](references/create-near-app.md)
2. Run scaffolding:

```bash
npx create-near-app@latest
```

Or with arguments:

```bash
npx create-near-app my-app --frontend vite-react --contract rs --install
```

**Framework options:** `vite-react` | `next-app` | `next-page`
**Contract options:** `rs` (Rust) | `ts` (TypeScript)

3. If user needs wallet connection in the scaffolded app, also follow Path B.

### Path B: Existing Project

User wants to add NEAR wallet connection to an existing React app.

1. Read [references/near-connect-hooks.md](references/near-connect-hooks.md) — React hooks API and patterns
2. If user needs low-level wallet API or non-React integration, also read [references/near-connect.md](references/near-connect.md)

**Quick setup:**

```bash
npm install near-connect-hooks @hot-labs/near-connect near-api-js
```

```tsx
// 1. Wrap app root with NearProvider
import { NearProvider } from 'near-connect-hooks';

<NearProvider config={{ network: 'mainnet' }}>
  <App />
</NearProvider>

// 2. Use hook in components
import { useNearWallet } from 'near-connect-hooks';

const { signedAccountId, signIn, signOut, viewFunction, callFunction } = useNearWallet();
```

### Path C: Non-React or Vanilla JS

Use `@hot-labs/near-connect` directly without React hooks.

1. Read [references/near-connect.md](references/near-connect.md)

```bash
npm install @hot-labs/near-connect
```

```typescript
import { NearConnector } from "@hot-labs/near-connect";

const connector = new NearConnector({ network: "mainnet" });
connector.on("wallet:signIn", async (t) => {
  const wallet = await connector.wallet();
  const address = t.accounts[0].accountId;
});
await connector.connect();
```

## Key Patterns

- **Read contract state** (no wallet): `viewFunction({ contractId, method, args })`
- **Write to contract** (wallet required): `callFunction({ contractId, method, args, deposit })`
- **Transfer NEAR**: `transfer({ receiverId, amount })`
- **NEAR ↔ yoctoNEAR**: Use `nearToYocto()` / `yoctoToNear()` from `near-api-js`
- **1 NEAR** = `"1000000000000000000000000"` yoctoNEAR
- **Default gas**: `"30000000000000"` (30 TGas)

Overview

This skill helps you build NEAR Protocol dApps and integrate wallet functionality into web apps. It supports scaffolding new projects with create-near-app, adding React wallet hooks via near-connect-hooks, and using the low-level @hot-labs/near-connect API for non-React or advanced flows. You can read contract state, call transactions, transfer NEAR, and handle signing/connection flows.

How this skill works

For new projects, run the create-near-app scaffolder to generate Vite+React or Next.js templates and optional Rust/TypeScript contracts. For existing apps, install near-connect-hooks and wrap your root with NearProvider, then use useNearWallet() to sign in/out, read view functions, and call contract methods. For vanilla JS or non-React environments, use @hot-labs/near-connect to create a NearConnector, subscribe to wallet events, and call wallet/connect APIs directly.

When to use it

  • Create a new NEAR dApp quickly with a configured frontend and contract template.
  • Add NEAR wallet sign-in/sign-out, contract calls, and transaction signing to an existing React app.
  • Integrate NEAR features into non-React or vanilla JavaScript projects using a connector.
  • Perform contract reads (view) without requiring user wallet access.
  • Execute write operations (contract calls, transfers) that require user signatures.

Best practices

  • Develop and test on testnet before deploying to mainnet to avoid lost funds.
  • Wrap the app root with NearProvider when using React hooks to centralize config.
  • Handle async errors and wallet popups gracefully; always check signedAccountId before write calls.
  • Use near-api-js utilities for reliable conversions between NEAR and yoctoNEAR.
  • Supply appropriate gas and deposit for contract calls; default gas is 30000000000000 (30 TGas).

Example use cases

  • Scaffold a new Vite+React NEAR dApp with a Rust contract using create-near-app.
  • Add a Sign in with NEAR button and signer flows to an existing React marketplace using near-connect-hooks.
  • Implement a token transfer UI that converts NEAR to yoctoNEAR before calling transfer().
  • Integrate a serverless function that reads contract state with viewFunction and displays balances.
  • Build a custom wallet flow in a vanilla JS site using NearConnector and wallet event listeners.

FAQ

Do I need a wallet to read contract state?

No. Use viewFunction({ contractId, method, args }) to read state without a wallet.

What packages do I install for a React integration?

Install near-connect-hooks, @hot-labs/near-connect, and near-api-js; wrap with NearProvider and use useNearWallet().