Skip to main content

Quick Reference

info

Here is a collection of the most commonly used methods within Subsocial SDK. For more in-depth look into this library, please reference the TypeDocs.

Setup#

Install#

yarn add @subsocial/api

Add type definitions and utils library:

yarn add @subsocial/types @subsocial/definitions @subsocial/utils

Import#

import { newFlatSubsocialApi } from '@subsocial/api'

Configuration#

const config = {
substrateNodeUrl: 'wss://testnet.subsocial.network',
offchainUrl: 'https://staging.subsocial.network/offchain',
ipfsNodeUrl: 'https://staging.subsocial.network/ipfs'
}
const flatApi = await newFlatSubsocialApi(config)

Reading Data#

Space#

Space is the place where all content on SubSocial lives. It holds multiple posts from different people depending upon the permission. Read More

const spaceId = 1
const space = await flatApi.findSpace({id: spaceId})

Check full docs here.

Post#

Post is the piece of content that provides value for the readers. It can be some written text, an image, or a video. Read More

const postId = 1
const post = await flatApi.findPost(postId)

Check full docs here.

Profile#

Profile is linked to your Subsocial account address, and is an overview of your activity on Subsocial. You can set a profile picture and a username for your account, as well as a personal website link. Read More

const accountId = '<account_public_key>'
const profile = await flatApi.findProfile(accountId)

Check full docs here.

Writing Data#

Space#

Add following import statement:

import {
IpfsContent,
OptionBool,
SpaceUpdate
} from "@subsocial/types/substrate/classes"

Storing space details in IPFS, and generating a CID.

const ipfsImageCid = await api.subsocial.ipfs.saveFile(file)
const cid = await ipfs.saveContent({
about: 'Subsocial is an open protocol for decentralized social networks and marketplaces. It`s built with Substrate and IPFS',
image: ipfsImageCid,
name: 'Subsocial',
tags: [ 'subsocial' ]
})

Creating a Space transaction object

const substrateApi = await api.subsocial.substrate.api
const spaceTransaction = substrateApi.tx.spaces.createSpace(
null, // Parent Id (optional)
null, // Handle name (optional)
IpfsContent(cid),
null // Permissions config (optional)
)

Signing a transaction and sending to blockchains

const keyring = (new Keyring({ type: "sr25519" }))
const senderKeyPair = keyring.addFromMnemonic('<your_mnemonic_here>')
spaceTransaction.signAndSend(senderKeyPair, async (result) => {
const { status } = result
if(!result || !status){
return;
}
if (status.isFinalized || status.isInBlock) {
const blockHash = status.isFinalized
? status.asFinalized
: status.asInBlock;
console.log('โœ… Tx finalized. Block hash', blockHash.toString());
} else if (result.isError) {
console.log(JSON.stringify(result));
} else {
console.log('โฑ Current tx status:', status.type);
}
})

Check full docs here.

Post#

Add following import statement:

import {
IpfsContent,
OptionBool,
SpaceUpdate
} from "@subsocial/types/substrate/classes"

Storing post details in IPFS, and generating a CID.

const ipfsImageCid = await api.subsocial.ipfs.saveFile(file)
const cid = await ipfs.saveContent({
title: "What is Subsocial?",
image: ipfsImageCid,
tags: [ 'Hello world', 'FAQ' ],
body: 'Subsocial is an open protocol for decentralized social networks and marketplaces. It`s built with Substrate and IPFS.'
})

Creating a post transaction object

const spaceId = '1' // The space in which you're posting.
const substrateApi = await api.subsocial.substrate.api
const postTransaction = substrateApi.tx.posts.createPost(
spaceId,
{ RegularPost: null }, // Creates a regular post.
IpfsContent(cid)
)

Signing a transaction and sending to blockchain

const keyring = (new Keyring({ type: "sr25519" }))
const senderKeyPair = keyring.addFromMnemonic('<your_mnemonic_here>')
postTransaction.signAndSend(senderKeyPair, async (result) => {
const { status } = result
if(!result || !status){
return;
}
if (status.isFinalized || status.isInBlock) {
const blockHash = status.isFinalized
? status.asFinalized
: status.asInBlock;
console.log('โœ… Tx finalized. Block hash', blockHash.toString());
} else if (result.isError) {
console.log(JSON.stringify(result));
} else {
console.log('โฑ Current tx status:', status.type);
}
})

Check full docs here.

Profile#

Add the following import statement:

import { IpfsContent } from "@subsocial/types/substrate/classes"

Storing profile details in IPFS, and generating a CID to add on blockchain:

const ipfsImageCid = await api.subsocial.ipfs.saveFile(file)
const cid = await ipfs.saveContent({
about: 'Subsocial official account.',
avatar: ipfsImageCid,
name: 'Subsocial',
})

Creating profile transaction object:

const substrateApi = await api.subsocial.substrate.api
const profileTransaction = substrateApi.tx.profiles.createProfile(
IpfsContent(cid)
)

Signing a transaction and sending to blockchain

const keyring = (new Keyring({ type: "sr25519" }))
const senderKeyPair = keyring.addFromMnemonic('<your_mnemonic_here>')
profileTransaction.signAndSend(senderKeyPair, async (result) => {
const { status } = result
if(!result || !status){
return;
}
if (status.isFinalized || status.isInBlock) {
const blockHash = status.isFinalized
? status.asFinalized
: status.asInBlock;
console.log('โœ… Tx finalized. Block hash', blockHash.toString());
} else if (result.isError) {
console.log(JSON.stringify(result));
} else {
console.log('โฑ Current tx status:', status.type);
}
})

Check full docs here.

Comments#

Comments are replies to a post that are visible below a post.

Reading Comments#

import { idToBn } from "@subsocial/utils"
const substrate = flatApi.subsocial.substrate
const postId = '1'
// Get reply ids (comments) by parent post id and fetch posts by ids
const replyIds = await substrate.getReplyIdsByPostId(idToBn(postId))
// For getting comments use posts functions
const replies = await flatApi.findPublicPosts(replyIds)

Writing Comments#

import { IpfsContent } from "@subsocial/types/substrate/classes"
const spaceId = '1' // Optional.
const rootPostId = '1'
const cid = await ipfs.saveContent({
body: 'Keep up the good work!'
})
const substrateApi = flatApi.subsocial.substrate
const tx = await substrateApi.tx.posts.createPost(spaceId, { Comment: { parentId: null, rootPostId}}, IpfsContent(cid))

Check full docs here.

Follows#

Check if follower#

This checks if an account is following a particular space.

const accountId = '<any_public_key>'
const spaceId = '1'
const substrateApi = flatApi.subsocial.substrate
const isFollower = await substrateApi.isSpaceFollower(accountId, spaceId)

Fetch list of followers#

For Spaces#

import { bnsToIds } from '@subsocial/utils'
const spaceId = '1'
const substrateApi = flatApi.subsocial.substrate
const res = await (await substrateApi.api).query.spaceFollows.spaceFollowers(spaceId)
const followersSpaceIds = bnsToIds(res)

For Accounts#

import { bnsToIds } from '@subsocial/utils'
const accountId = '<any_public_key>'
const substrateApi = flatApi.subsocial.substrate
const res = await (await substrateApi.api).query.profileFollows.accountFollowers(accountId)
const followersOfAccount = bnsToIds(res)

Check full docs here.

Follow / Unfollow#

For Spaces#

const spaceId = '1'
const substrateApi = flatApi.subsocial.substrate
const tx = substrateApi.tx.spaceFollows.followSpace(spaceId)

Signing a transaction and sending to blockchain

const keyring = (new Keyring({ type: "sr25519" }))
const senderKeyPair = keyring.addFromMnemonic('<your_mnemonic_here>')
tx.signAndSend(senderKeyPair, async (result) => {
const { status } = result
if(!result || !status){
return;
}
if (status.isFinalized || status.isInBlock) {
const blockHash = status.isFinalized
? status.asFinalized
: status.asInBlock;
console.log('โœ… Tx finalized. Block hash', blockHash.toString());
} else if (result.isError) {
console.log(JSON.stringify(result));
} else {
console.log('โฑ Current tx status:', status.type);
}
})

For Accounts#

const accountIdToFollow = '<any_public_key>'
const substrateApi = flatApi.subsocial.substrate
const tx = substrateApi.tx.profileFollows.followAccount(accountIdToFollow)

Signing a transaction and sending to blockchain

const keyring = (new Keyring({ type: "sr25519" }))
const senderKeyPair = keyring.addFromMnemonic('<your_mnemonic_here>')
tx.signAndSend(senderKeyPair, async (result) => {
const { status } = result
if(!result || !status){
return;
}
if (status.isFinalized || status.isInBlock) {
const blockHash = status.isFinalized
? status.asFinalized
: status.asInBlock;
console.log('โœ… Tx finalized. Block hash', blockHash.toString());
} else if (result.isError) {
console.log(JSON.stringify(result));
} else {
console.log('โฑ Current tx status:', status.type);
}
})

Check full docs here.

Reactions#

Reactions are your signs to Upvote or Downvote a post.

Get all reactions#

const myAccount = '<any_account_public_key>';
const reaction = await flatApi.substrate.getPostReactionIdByAccount (myAccount, '1')

Reacting to a post#

const postId = '1' // Post Id you want to react on.
const substrateApi = flatApi.subsocial.substrate
const reactionTx = substrateApi.tx.reactions.createPostReaction(postId, 'Upvote')

Signing a transaction and sending to blockchain

const keyring = (new Keyring({ type: "sr25519" }))
const senderKeyPair = keyring.addFromMnemonic('<your_mnemonic_here>')
tx.signAndSend(senderKeyPair, async (result) => {
const { status } = result
if(!result || !status){
return;
}
if (status.isFinalized || status.isInBlock) {
const blockHash = status.isFinalized
? status.asFinalized
: status.asInBlock;
console.log('โœ… Tx finalized. Block hash', blockHash.toString());
} else if (result.isError) {
console.log(JSON.stringify(result));
} else {
console.log('โฑ Current tx status:', status.type);
}
})

Check full docs here.