The Swoop SDK (@swoopads/sdk)
Units in code, for React and plain JavaScript.
@swoopads/sdk is the same unit as typed functions, plus a React component, a web component and a small server helper. It replaces @gullylabs/ads, which stays as a thin copy that re-exports it.
A unit without a backend#
import { mountUnit } from '@swoopads/sdk'
const unit = mountUnit(document.getElementById('sponsor'), {
spot: 'pk_YOUR_SPOT_KEY',
unit: 'native', // banner | text | native
size: '480x160', // one listed size, exactly (or the biggest that fits)
theme: 'auto', // light | dark | auto
onState: (state, { reason }) => console.log(state, reason),
})
// unit.refresh(), unit.destroy()import { SwoopUnit } from '@swoopads/sdk/react'
export function Sponsor() {
return <SwoopUnit spot="pk_YOUR_SPOT_KEY" unit="banner" size="728x90" theme="dark" />
}import { loadTag } from '@swoopads/sdk'
// optional integrity pin: see the tag page for the current hash
const Swoop = await loadTag({ src: 'https://swoopads.xyz/t/1.1.0.js', integrity: 'sha384-TdTzA4ueOm72L7h6secO0fNlEegFSFA6ea7adoYFg4zAQ2203PAUwP1b2Kw0DCsg' })mountUnit returns null and does nothing on a server, so it is safe in server-rendered code. In React, the component draws an empty box and fills it in the browser. It never throws; a callback of yours that throws is caught.
Options#
| Option | Values | Default |
|---|---|---|
| spot | your public Spot key | none |
| unit | banner, text, native | native |
| theme | light, dark, auto | auto |
| slot | a spot id | by kind |
| size | a listed size such as 728x90 | the biggest that fits |
| width | widest size it may take | the element width |
| block | a CSS selector that keeps ads off the page | none |
| page, creator, content, topic | text | none |
| baseUrl | the API address | offers.kre8.fun |
| onState | callback (state, { reason }) | none |
The signed client and the web component#
If you draw the ad yourself, the client asks for the ad, counts the view and click, and signs requests on your server so the Spot secret never reaches the browser. The element <swoop-ad-slot> (also <gully-ad-slot>) does this for you.
import { createServeSigner } from '@swoopads/sdk/server'
const sign = createServeSigner({
siteId: process.env.SWOOP_SPOT_KEY!, // pk_…
siteKey: process.env.SWOOP_SPOT_SECRET!, // sk_… never in the browser
})
// POST /api/swoop/sign -> return await sign(req.body)<script type="module">import '@swoopads/sdk/define'</script>
<swoop-ad-slot slot="home_feed" size="480x160" site="pk_YOUR_SPOT_KEY" sign-url="/api/swoop/sign"></swoop-ad-slot>Moving from @gullylabs/ads#
- Nothing breaks: @gullylabs/ads keeps working and re-exports everything from @swoopads/sdk.
- New code should import from @swoopads/sdk (same names, plus SwoopAds for GullyAds).
The source and tests are in the open Gully Labs SDK repository, MIT licensed.