Node.js SDK
Reference for the Node.js SDK.
Serves ads from a server: a route handler, a server component, a background job.
Use the browser SDK where you can
Serving from a server costs you signal, and some of it is not recoverable:
- No identity cookie, so a returning visitor without a wallet can't be recognised. You fill only against the addresses you pass.
- Geolocation doesn't work, so geofenced / geo-targeted campaigns will not be served correctly.
Use it when a browser call is genuinely not an option. Otherwise reach for the Browser SDK.
Runnable example
node-typescript is this page as a working Node.js app.
1. Install
npm install @specify-sh/publisher-sdk2. Serve an ad
import { serve, ImageFormat } from '@specify-sh/publisher-sdk/server';
const ad = await serve({
publisherKey: process.env.SPECIFY_PUBLISHER_KEY!,
walletAddresses: ['0x1111111111111111111111111111111111111111'],
imageFormat: ImageFormat.LANDSCAPE,
adUnitId: 'sidebar'
});
if (ad) {
console.log(ad.headline, ad.ctaUrl);
}Options
| Option | Required | Description |
|---|---|---|
publisherKey | yes | Your key, spk_ followed by 30 characters |
walletAddresses | yes | One or more addresses. Up to 50 are sent, and anything that isn't an EVM address, such as a Solana address, is skipped |
imageFormat | yes | LANDSCAPE, LONG_BANNER, SHORT_BANNER or NO_IMAGE |
adUnitId | no | An id of your choosing, so you can compare placements in reporting |
Image formats and SpecifyAd
Identical to the browser SDK: see image formats and SpecifyAd.
Errors
ValidationError is thrown for invalid input before making a request:
- a publisher key that is not
spk_followed by 30 characters - an empty address list
Addresses never throw: anything that is not 0x plus 40 hex characters is skipped, and serve() returns null when none is left. Everything else returns null.
import { serve, ValidationError } from '@specify-sh/publisher-sdk/server';
try {
const ad = await serve({ publisherKey, walletAddresses, imageFormat });
} catch (error) {
if (error instanceof ValidationError) {
console.error(error.message);
}
}