Specify docs

Browser SDK

Reference for the Specify Browser SDK.

npm i @specify-sh/publisher-sdk

new Specify(config)

OptionRequiredDescription
publisherKeyyesYour key, which starts with spk_

Throws ValidationError on a malformed key.

serve()

serve(options): Promise<SpecifyAd | null>
serve(addresses, options): Promise<SpecifyAd | null>

Returns an ad, or null when there is nothing to show.

addresses takes one address or an array of them. Pass every wallet the visitor has connected: anything that isn't an EVM address, such as a Solana address, is skipped. They go first, and addresses registered through identify() fill the rest, up to 50 in total.

Pass null or undefined in the address position, or omit it, to rely on the registered addresses and, with consent, Specify's cookie. With neither, serve() returns null. So serve(wallet ?? null, options) covers a maybe-connected wallet in one call.

OptionRequiredDescription
imageFormatyesSee image formats
adUnitIdnoAn id of your choosing, so you can compare placements in reporting

serve() never throws for a failed request. A no-fill, an outage on our side and a network error all return null, so it can't break your page. It throws only for invalid input; see errors.

identify(addresses)

Registers one or more addresses to include in every later serve().

The client keeps the 50 most recently registered addresses and never removes one, including on sign-out. Anything that isn't an EVM address is skipped. Does nothing outside a browser.

onIdentityChange(listener)

Calls listener when a later serve() could do better: identify() registers an address that was not already there, or setCookieConsent(true) grants consent. Returns a function that detaches it.

Withdrawing consent does not run it.

Use it to fill a slot that came back empty:

const stop = specify.onIdentityChange(serveAd);
setCookieConsent(granted: boolean): void
hasCookieConsent(): boolean

Sets and reads the visitor's consent for Specify's cookie, which lets a placement fill for a returning visitor without a wallet. setCookieConsent() does nothing outside a browser, such as during a server render.

Consent starts false and the SDK never stores it, so set it on every page load, before your first serve. A withdrawal takes effect on the next serve(). If you have no consent platform, store the visitor's choice yourself.

Without consent, ads still serve against the addresses you pass.

Image formats

FormatRatioPixelsBest for
LANDSCAPE16:9640 × 360Hero banners, featured placements
LONG_BANNER8.09:11456 × 180Header and footer placements, leaderboards
SHORT_BANNER16:5640 × 200Inline content, sidebars, mobile banners
NO_IMAGEn/atext onlyDense lists, text feeds, places an image would not fit

Preview each format, and how an ad looks under different configurations, in the playground.

SpecifyAd

PropertyTypeDescription
campaignIdstringUnique identifier for the ad campaign
adIdstringUnique identifier for this specific ad
headlinestringAd headline text (plain text, no markdown)
contentstringAd body content. Supports simplified markdown. Max 500 characters
ctaUrlstringCall-to-action URL
ctaLabelstringCall-to-action button text (plain text, no markdown)
imageUrlstring | nullURL to the ad image (null for NO_IMAGE placements)
communityNamestringName of the advertising community (plain text, no markdown)
communityLogostringURL to the community logo
imageFormatstringThe format returned (LANDSCAPE, LONG_BANNER, SHORT_BANNER, or NO_IMAGE)
adUnitIdstringThe adUnitId from the request, or an empty string if none was sent

Link your call-to-action to ctaUrl exactly as given.

Content formatting

content supports a small subset of markdown. Everything else is plain text.

FormatSyntaxExample
Bold**text****Join today**
Italic*text**Italic*
Underline__text____Out now__
Bullet points* item* Feature 1\n* Feature 2
Line breaks\nFirst line\nSecond line

Errors

ValidationError is thrown for invalid input before making a request:

  • a publisher key that is not spk_ followed by 30 characters
  • serve() called without an options object

Addresses never throw: anything that is not 0x plus 40 hex characters is skipped. Everything else returns null.

import { ImageFormat, ValidationError } from '@specify-sh/publisher-sdk';

try {
  const ad = await specify.serve(addresses, { imageFormat: ImageFormat.LANDSCAPE });
} catch (error) {
  if (error instanceof ValidationError) {
    console.error(error.message);
  }
}

On this page