The Specify Publisher SDK provides access to targeted advertising content based on end user wallet addresses. This JavaScript/TypeScript SDK allows publishers to serve personalized ads to users based on their Ethereum or EVM-compatible wallet addresses.
importSpecify,{ImageFormat}from'@specify-sh/sdk';// Initialize the SDKconstspecify = newSpecify({publisherKey:'spk_your_publisher_key_here'});// Serve an ad to a wallet addresstry{constad = awaitspecify.serve('0x742d35Cc6634C0532925a3b8D57C11E4a3e1A510',ImageFormat.LANDSCAPE);if(ad){console.log('Ad content:',ad);console.log('Headline:',ad.headline);console.log('Content:',ad.content);console.log('Image URL:',ad.imageUrl);console.log('CTA:',ad.ctaLabel,'->',ad.ctaUrl);}else{console.log('No ad found for this wallet');}}catch(error){console.error('Error serving ad:',error);}
import{ImageFormat}from'@specify-sh/sdk';// For hero sections or main content areasconstheroAd = awaitspecify.serve(walletAddress,ImageFormat.LANDSCAPE);// For website headers or newsletter banners constheaderAd = awaitspecify.serve(walletAddress,ImageFormat.LONG_BANNER);// For sidebar or inline placementsconstsidebarAd = awaitspecify.serve(walletAddress,ImageFormat.SHORT_BANNER);
Thrown when no ad content is found for the specified wallet address(es).
try{constad = awaitspecify.serve('0x742d35Cc6634C0532925a3b8D57C11E4a3e1A510',ImageFormat.LANDSCAPE);}catch(error){if(errorinstanceofNotFoundError){console.log('No ad available for this wallet');}}
Always wrap SDK calls in try-catch blocks and handle specific error types:
import{AuthenticationError,ValidationError,NotFoundError,APIError,ImageFormat}from'specify-publisher-sdk';try{constad = awaitspecify.serve(walletAddress,ImageFormat.LANDSCAPE);if(ad){// Display the addisplayAd(ad);}else{// Handle no ad caseshowFallbackContent();}}catch(error){if(errorinstanceofValidationError){console.error('Invalid input:',error.message);}elseif(errorinstanceofNotFoundError){console.log('No ad available');showFallbackContent();}elseif(errorinstanceofAuthenticationError){console.error('Authentication failed. Check your publisher key.');}elseif(errorinstanceofAPIError){console.error('API error:',error.message);showErrorMessage();}else{console.error('Unexpected error:',error);}}
importexpressfrom'express';importSpecify,{ValidationError,NotFoundError,ImageFormat}from'specify-publisher-sdk';constapp = express();constspecify = newSpecify({publisherKey:process.env.SPECIFY_PUBLISHER_KEY!
});app.get('/api/ads/:walletAddress',async(req,res)=>{try{const{walletAddress} = req.params;const{format = 'LANDSCAPE'} = req.query;constad = awaitspecify.serve(walletAddress,formatas ImageFormat);if(ad){res.json({success:true,ad:{...ad,// You can transform or add additional fields heretimestamp:newDate().toISOString()}});}else{res.status(404).json({success:false,message:'No ad found'});}}catch(error){if(errorinstanceofValidationError){res.status(400).json({success:false,message:error.message,details:error.details});}elseif(errorinstanceofNotFoundError){res.status(404).json({success:false,message:'No ad found'});}else{console.error('Server error:',error);res.status(500).json({success:false,message:'Internal server error'});}}});app.listen(3000,()=>{console.log('Server running on port 3000');});