Getting started with SDK

It's a Quick Start Guide to start using Multichain SDK.

We will install the SDK, instantiate it, do simple API queries and list an NFT for sale.

Installation

Installing Rarible SDK

yarn add @rarible/sdk
yarn add [email protected]

Creating SDK instance

import { createRaribleSdk } from "@rarible/sdk"

// Replace XXXXX with the API key which you got on https://rarible.org
// undefined - is wallet instance. You can pass undefined to ignore wallet connection at this stage
const sdk = createRaribleSdk(undefined, "prod", { apiKey: "XXXXX" })

Querying API

Here we are going to do several simple requests just to check how to use API clients.

import { Collection } from "@rarible/api-client"
import { Item } from "@rarible/api-client"

// Here we're loading collection information. replace X with collection id, usually contract address
const collection: Collection = await sdk.apis.collection.getCollectionById({ collection: "ETHEREUM:X" })

// Here we'are loading NFT information and metadata. replace X with contract address and y with tokenId
const nft: Item = await sdk.apis.item.getItemById({ itemId: "ETHEREUM:X:Y" })

You can find more information about possible queries in our OpenAPI documentation

Interacting with wallets

Interacting with EVM wallets

API usually allows you to read data about NFTs, but to interact with the blockchain you need a wallet.

To use the wallet you should install ethers.js or web3.js first (for EVMs)

#install ethers.js
yarn add [email protected]

#or install web3.js
yarn add [email protected]

#you can choose what library to use, we support both options

Then you should instantiate web3.js instance or ethers.js signer and create Rarible SDK using it

// signer - ethers.js Signer. You can pass web3.js instance as well
const sdk = createRaribleSdk(signer, "prod", { apiKey: "XXXXX" })

Interacting with Flow wallets

You can instantiate Rarible SDK using FCL

yarn add @onflow/fcl

After that you can create SDK by passing fcl instance into createRaribleSdk function

import * as fcl from "@onflow/fcl"

fcl.config({
  "accessNode.api": "https://access-testnet.onflow.org", // Mainnet: "https://access-mainnet-beta.onflow.org"
  "discovery.wallet": "https://fcl-discovery.onflow.org/testnet/authn" // Mainnet: "https://fcl-discovery.onflow.org/authn"
})
const sdk = createRaribleSdk(fcl, "prod")

Creating Rarible SDK for ImmutableX

Create ImxWallet, invoke connect and pass the wallet to createRaribleSdk function

import { ImxWallet } from "@rarible/immutable-wallet"

const imxConnectorWallet = new ImxWallet("prod")
await imxConnectorWallet.connect()
const sdk = createRaribleSdk(imxConnectorWallet, "prod")

Using Rarible SDK to list an NFT token for sale

Now Rarible SDK has a wallet to interact with and you can call all available functions to interact with NFTs.
For example, here we are going to list an NFT for sale. To do this you need to own this NFT:

import { toCurrencyId, toItemId, toOrderId } from "@rarible/types"

// This will list an NFT for 0.5 ETH
// Replace X with contract address and Y with tokenId
const orderId = await sdk.order.sell({
  itemId: toItemId("ETHEREUM:X:Y"),
  amount: 1,
  currency: toCurrencyId("ETHEREUM:native"),
  price: "0.5"
})