Skip to main content

SDK Quick start

The Media Protocol SDK exists to help developers build on top of Media Protocol. It's designed to run in any environment that can execute JavaScript (think websites, node scripts, etc.). While simple enough to use in a hackathon project, it's also robust enough to power production applications.

Installation

The easiest way to consume the SDK is via npm. To install it in your project, simply run yarn add media-sdk (or npm install media-sdk).

Usage

To run code from the SDK in your application, use an import or require statement, depending on which your environment supports. Note that the guides following this page will use ES6 syntax.

ES6 (import)

import { Sdk, MarketplaceViewer, Marketplace, Resources, MarketplaceHelper } from 'media-sdk';

CommonJS (require)

const { Sdk, MarketplaceViewer, Marketplace, Resources, MarketplaceHelper } = require('media-sdk');

Initializing the SDK

To initialize an instance of the Media SDK, use the Sdk class. This class constructor takes in an object with the following optional parameters:

  • chain: A chain object. See Viem's Chains for more details. If nothing is provided, the default chain will be used, which is Ethereum Sepolia until mainnet launch.
  • transport: An array of transport objects. See Viem's Transport for more details.
  • privateKey: A ECP256K1 private key as a hex string to create a wallet client. Example: 'afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890'.
  • mnemonic: A BIP39 mnemonic phrase to create a wallet client. Example: 'degree tackle suggest window test behind mesh extra cover prepare oak script'.
  • walletClient: A wallet client. See Viem's Wallet Client for more details.

All parameters are optional. If all three privateKey, mnemonic, and walletClient are absent, only view functions will be available. Therefore, none write functions will be available.

Examples

Using a private key.

Useful for backend applications

import { Sdk } from 'media-sdk'

// initialize the sdk using a private key.
const sdk = new Sdk({ privateKey: 'afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890' })

Using a mnemonic.

import { Sdk } from 'media-sdk'

// initialize the sdk using a mnemonic.
const sdk = new Sdk({ mnemonic: 'degree tackle suggest window test behind mesh extra cover prepare oak script' })

Using a browser wallet:

import { Sdk, custom, createWalletClient, validChains } from 'media-sdk'

const [account] = await window.ethereum.request({method: 'eth_requestAccounts'})

const walletClient = createWalletClient({
account: account,
chain: validChains[Number(window.ethereum.chainId)],
transport: [custom(window.ethereum)]
})

// initialize the SDK using a Viem walletClient.
const sdk = new Sdk({ walletClient: walletClient });

Using it without signer

Useful to use the view functions only.

import { Sdk, MarketplaceViewer, validChains } from 'media-sdk'

const sdk = new Sdk({ chain: validChains[85432] })

const marketplaceViewer = new MarketplaceViewer(sdk)

const result = await marketplaceViewer.getPaginatedOffers({
marketplaceId: 1,
start: 0,
count: 100
})
console.log(result)

Reference

Comprehensive reference material for the SDK can be found here

note

Please note that this documentation is currently a work in progress, and we apologize for any errors or blank pages you may come across. We welcome any contributions or feedback to improve the documentation via the official repository at https://github.com/mediafoundation/media-protocol-docs.