Browser SDK
Reference for the Specify Browser SDK.
npm i @specify-sh/publisher-sdknew Specify(config)
| Option | Required | Description |
|---|---|---|
publisherKey | yes | Your 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.
| Option | Required | Description |
|---|---|---|
imageFormat | yes | See image formats |
adUnitId | no | An 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);Consent
setCookieConsent(granted: boolean): void
hasCookieConsent(): booleanSets 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
| Format | Ratio | Pixels | Best for |
|---|---|---|---|
LANDSCAPE | 16:9 | 640 × 360 | Hero banners, featured placements |
LONG_BANNER | 8.09:1 | 1456 × 180 | Header and footer placements, leaderboards |
SHORT_BANNER | 16:5 | 640 × 200 | Inline content, sidebars, mobile banners |
NO_IMAGE | n/a | text only | Dense lists, text feeds, places an image would not fit |
Preview each format, and how an ad looks under different configurations, in the playground.
SpecifyAd
| Property | Type | Description |
|---|---|---|
campaignId | string | Unique identifier for the ad campaign |
adId | string | Unique identifier for this specific ad |
headline | string | Ad headline text (plain text, no markdown) |
content | string | Ad body content. Supports simplified markdown. Max 500 characters |
ctaUrl | string | Call-to-action URL |
ctaLabel | string | Call-to-action button text (plain text, no markdown) |
imageUrl | string | null | URL to the ad image (null for NO_IMAGE placements) |
communityName | string | Name of the advertising community (plain text, no markdown) |
communityLogo | string | URL to the community logo |
imageFormat | string | The format returned (LANDSCAPE, LONG_BANNER, SHORT_BANNER, or NO_IMAGE) |
adUnitId | string | The 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.
| Format | Syntax | Example |
|---|---|---|
| Bold | **text** | **Join today** |
| Italic | *text* | *Italic* |
| Underline | __text__ | __Out now__ |
| Bullet points | * item | * Feature 1\n* Feature 2 |
| Line breaks | \n | First 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);
}
}