npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

sponsored-messaging-sdk

v1.0.0

Published

SDK for integrating sponsored content into chat applications

Readme

Sponsored Messaging SDK

SDK for interacting with the Sponsored Messaging API, with support for multiple JavaScript frameworks.

Installation

npm install sponsored-messaging-sdk
# or
yarn add sponsored-messaging-sdk

Usage

Vanilla JavaScript

ES Module Import

import SponsoredMessaging from 'sponsored-messaging-sdk';

const sdk = new SponsoredMessaging({
  apiBaseUrl: 'https://your-api-url.com',
  sourceAppId: 'your-app-id',
  chatContainerId: 'chatArea',
  sponsorshipMode: 'chat' // or 'message'
});

// Check sponsorship for a message
async function checkMessageSponsorship(userMessage, aiResponse) {
  try {
    const result = await sdk.checkSponsorship({
      userMessage,
      aiResponse
    });
    
    console.log('Is sponsored:', result.isSponsored);
    if (result.isSponsored) {
      console.log('Sponsor details:', result.sponsorDetails);
    }
    
    return result;
  } catch (error) {
    console.error('Error checking sponsorship:', error);
    return { isSponsored: false };
  }
}

Script Tag (UMD)

<script src="https://cdn.example.com/sponsored-messaging-sdk.umd.min.js"></script>
<script>
  const sdk = new SponsoredMessaging({
    apiBaseUrl: 'https://your-api-url.com',
    sourceAppId: 'your-app-id',
    chatContainerId: 'chatArea',
    sponsorshipMode: 'chat'
  });
  
  // Use the SDK as needed
</script>

React Integration

import React from 'react';
import { SponsoredMessagingProvider, useSponsoredMessaging } from 'sponsored-messaging-sdk';

// Wrap your app or component with the provider
function App() {
  return (
    <SponsoredMessagingProvider
      apiBaseUrl="https://your-sponsorship-api.com"
      sourceAppId="my-react-app"
      chatContainerId="chat-container"
      sponsorshipMode="message"
    >
      <ChatComponent />
    </SponsoredMessagingProvider>
  );
}

// Use the SDK in any child component
function ChatComponent() {
  const sdk = useSponsoredMessaging();
  const [messages, setMessages] = useState([]);
  
  const sendMessage = async (userMessage) => {
    // Add user message to chat
    setMessages(prev => [...prev, { type: 'user', content: userMessage }]);
    
    // Simulate AI response
    const aiResponse = `This is a response about "${userMessage}"`;
    
    try {
      // Check sponsorship
      const result = await sdk.checkSponsorship({
        userMessage,
        aiResponse,
        userSessionId: localStorage.getItem('user_session_id')
      });
      
      // Handle the result
      if (result.isSponsored) {
        setMessages(prev => [...prev, { 
          type: 'sponsored', 
          content: result.rawContent,
          sponsorData: result
        }]);
      } else {
        setMessages(prev => [...prev, { 
          type: 'ai', 
          content: result.rawContent || aiResponse 
        }]);
      }
    } catch (error) {
      console.error('Error checking sponsorship:', error);
      setMessages(prev => [...prev, { type: 'ai', content: aiResponse }]);
    }
  };
  
  return (
    <div id="chat-container">
      {/* Chat UI */}
    </div>
  );
}

API Reference

new SponsoredMessaging(options)

Initializes the SDK.

  • options (Object): Configuration options.
    • apiBaseUrl (String, required): The base URL for the Sponsored Messaging API.
    • sourceAppId (String, required): Identifier for the source application using the SDK.
    • chatContainerId (String, required): The ID of the HTML element containing chat messages.
    • sponsorshipMode (String, optional): How to display sponsorship ('message' or 'chat'). Defaults to 'message'.

checkSponsorship(options)

Checks if a given AI response should be replaced with sponsored content.

  • options (Object): Details for the sponsorship check.
    • userMessage (String, required): The user's input message.
    • aiResponse (String, required): The AI's generated response.
    • userSessionId (String, optional): A unique identifier for the user's session.
    • sourceAppId (String, optional): An identifier for the application using the SDK.
  • Returns: Promise<Object> - Resolves with the API response or a default object { isSponsored: false, rawContent: aiResponse } on failure.

trackClick(options)

Tracks a user click on sponsored content.

  • options (Object): Details for tracking the click.
    • campaignId (String, required): The ID of the campaign associated with the clicked content.
    • userSessionId (String, optional): A unique identifier for the user's session.
    • sourceAppId (String, optional): An identifier for the application using the SDK.
  • Returns: Promise<void> - Resolves when the tracking request is sent. Errors are logged silently.

React Components

<SponsoredMessagingProvider>

React context provider that initializes the SDK and makes it available to child components.

  • Props:
    • apiBaseUrl (String, required): The base URL for the Sponsored Messaging API.
    • sourceAppId (String, required): Identifier for the source application using the SDK.
    • chatContainerId (String, required): The ID of the HTML element containing chat messages.
    • sponsorshipMode (String, optional): How to display sponsorship ('message' or 'chat'). Defaults to 'message'.
    • children (ReactNode): Child components that will have access to the SDK.

useSponsoredMessaging()

React hook to access the SDK instance from any component within a SponsoredMessagingProvider.

  • Returns: The initialized SponsoredMessaging instance.

Build Formats

The SDK is available in multiple formats:

  • ESM (dist/sponsored-messaging-sdk.esm.js): For modern bundlers and environments that support ES modules.
  • CommonJS (dist/sponsored-messaging-sdk.cjs.js): For Node.js and bundlers that support CommonJS.
  • UMD (dist/sponsored-messaging-sdk.umd.js): For direct use in browsers via script tags.
  • UMD (minified) (dist/sponsored-messaging-sdk.umd.min.js): Minified version for production use in browsers.