Getting Started
Installation
- npm
- Yarn
- pnpm
- Bun
npm install near-social-js
yarn add near-social-js
pnpm add near-social-js
bun add near-social-js
Basic Usage
Reading Data (No Wallet Required)
import { Social } from 'near-social-js';
const social = new Social();
// Get a user's profile
const profile = await social.getProfile('alice.near');
console.log(profile?.name); // "Alice"
// Get who someone is following
const following = await social.getFollowing('alice.near');
// Get likes on a post
const likes = await social.getLikes({
type: 'social',
path: 'alice.near/post/main',
blockHeight: 12345678,
});
Writing Data (Requires Wallet)
Writing data requires a signer. The SDK returns transaction builders that you can send:
import { Social } from 'near-social-js';
import { Near } from 'near-kit';
// Initialize with your Near instance
const near = new Near({ network: 'mainnet' });
const social = new Social({ near });
// Update profile
const tx = await social.setProfile('alice.near', {
name: 'Alice',
description: 'Hello world!',
});
await tx.send();
// Create a post
const postTx = await social.createPost('alice.near', {
text: 'My first post!',
});
await postTx.send();
// Follow someone
const followTx = await social.follow('alice.near', 'bob.near');
await followTx.send();
Configuration
Default (Mainnet)
const social = new Social();
// Uses social.near contract on mainnet
Testnet
const social = new Social({
network: 'testnet',
contractId: 'v1.social08.testnet',
});
Custom Near Instance
import { Near } from 'near-kit';
const near = new Near({
network: 'mainnet',
privateKey: 'ed25519:...',
});
const social = new Social({ near });
Using the Graph Class
For low-level contract access, use Graph directly:
import { Graph } from 'near-social-js';
const graph = new Graph();
// Read any data from the contract
const data = await graph.get({
keys: ['alice.near/profile/**', 'bob.near/widget/*'],
});
// Store custom data
const tx = await graph.set({
signerId: 'alice.near',
data: {
'alice.near': {
custom: {
myApp: { setting: 'value' },
},
},
},
});
await tx.send();