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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stackadapt/pa-typescript-sdk

v0.27.0

Published

StackAdapt's new Public API SDK allows you to use all of the functionality of the existing [GQL Public API](https://docs.stackadapt.com/) with built-in workflows, types and general endpoints that provide you similar flexibility. Additionally, all of Stack

Downloads

392

Readme

@stackadapt/pa-typescript-sdk

StackAdapt's new Public API SDK allows you to use all of the functionality of the existing GQL Public API with built-in workflows, types and general endpoints that provide you similar flexibility. Additionally, all of StackAdapt's GQL types and Schema are also available to be exported in this SDK, giving you access to all the data you need to build powerful integrations.

Table of Contents

Prerequisites

Ensure that you have the following versions installed.

  • Node: v18.12.1 and above
  • Pnpm: v8.15.1 and above
  • Nvm: v0.38.0 and above

Installation

You can install @stackadapt/pa-typescript-sdk using npm.

npm install @stackadapt/pa-typescript-sdk

GQL API Documentation

This SDK is built on top of StackAdapt's GQL Public API. We are continuously working on adding more documentation for the SDK to provide users with a better understanding of its usage. In the meantime, for more information, please refer to the following GQL documentations:

Example Usage

Initializing

// Importing library
import { initStackAdaptSDK } from "@stackadapt/pa-typescript-sdk";

// Can use 'SANDBOX' or 'PRODUCTION'
// You only need to initialize it once in your codebase
initStackAdaptSDK({
  token: MY_TOKEN,
  env: "SANDBOX",
});

General Endpoint

import {
  initStackAdaptSDK,
  generalEndpoint,
  UserError, // Part of StackAdapt's GQL types
} from "@stackadapt/pa-typescript-sdk";

initStackAdaptSDK({
  token: MY_TOKEN,
  env: "SANDBOX",
});

const getAdvertisers = async (first: number) => {
  type Result = { advertisers: { nodes: { id: number }[] } };
  const result = await generalEndpoint<Result>({
    query:
      "query Advertisers($firsts: Int!){ advertisers(first: $firsts) { nodes {id } } }",
    variables: { firsts: first },
  });
  return result;
};

const createAdvertiser = async (name: string) => {
  type Result = {
    createAdvertiser: {
      advertiser: { id: string; name: string };
      userErrors: UserError[];
    };
  };
  const results = await generalEndpoint<Result>({
    query:
      "mutation createNewAdvertiser($input: AdvertiserInput!){ createAdvertiser(input: $input){ advertiser { id name } userErrors { message path }  } }",
    variables: { input: { name: name } },
  });
  return results;
};

Create Creatives using URLs

import {
  initStackAdaptSDK,
  createCreativeByURL,
  ResourceType,
} from "@stackadapt/pa-typescript-sdk";

initStackAdaptSDK({
  token: MY_TOKEN,
  env: "SANDBOX",
});

const result = await createCreativeByURL({
  resourceType: ResourceType.Image,
  url: "https://dxt983tp420wz.cloudfront.net/images/other/careers_engineering_jack_yiu@2x-3e20599c3982ae946ba4037f72874416.jpg",
  fragment: "{ id ...on ImageCreative{ s3Url } }", // GQL fragment of UploadedCreative type, defaults to { id }. More information on what fields are available https://docs.stackadapt.com/graphql/reference#definition-UploadedCreative
});

Create Creatives using Local Files

The localPath parameter should support both absolute and relative paths, assuming the asset exists at the specified path.

import {
  initStackAdaptSDK,
  createCreativeByLocalFile,
  ResourceType,
} from "@stackadapt/pa-typescript-sdk";

initStackAdaptSDK({
  token: MY_TOKEN,
  env: "SANDBOX",
});

const result = await createCreativeByLocalFile({
  resourceType: ResourceType.Image,
  localPath: "./resources/assets/IMAGE_JPEG_300x300.jpeg",
  fragment: "{ id ...on ImageCreative{ s3Url } }", // GQL fragment of UploadedCreative type, defaults to { id }. More information on what fields are available https://docs.stackadapt.com/graphql/reference#definition-UploadedCreative
});

Use The Latest Schema

This SDK uses the latest StackAdapt Public API Schema which is updated weekly.

import {
  StackadaptSchema, // Exports as GraphQLSchema type
} from "@stackadapt/pa-typescript-sdk";

Insights Reporting

import {
  initStackAdaptSDK,
  // Current Exposed Insights Reporting
  getDomainsReport,
  getGeographicReport,
  DateRangeInput,
  InsightReportingFilters,
  Pagination,
} from "@stackadapt/pa-typescript-sdk";

initStackAdaptSDK({
  token: MY_TOKEN,
  env: "SANDBOX",
});

const filters: InsightReportingFilters = { advertiserIds: [1] };

const pagination: Pagination = {
  first: 1,
};

const date: DateRangeInput = {
  from: "2021-01-01",
  to: "2024-12-31",
};

// Domain Insight Reporting
const domainReportResult = await getDomainReport({
  filters: filters,
  pagination: pagination,
  date: date,
});

// Geographical Insight Reporting
const geoReportResult = await getGeographicReport({
  filters: filters,
  pagination: pagination,
  date: date,
});

Create Simple Engagement Display Campaign

Getting Started

  1. Advertiser

A new advertiser will be created if a name string is passed in. An existing advertiser will be used if an advertiser id is passed.

  1. Campaign Group

A new campaign group will be created each time a new campaign is created. Both the campaign group and campaign will share the same flight data based on flightStartTime, flightEndTime, flightLifetimeBudget, bidAmount. The campaign group will be created based on campaignGroupName (optional) if passed in, else it will have a default name.

  1. Campaign Ads

Follow Create Creatives using URLs, Create Creatives using Local Files to upload the creative and obtain if using Display Image Ads instead of JS Tags. This is optional depending on if the creative is already uploaded.

  1. Campaign Geo

This is an optional input, see more details in the StackAdapt Campaign Geo Targeting guide. The geo targeting must be of the custom type SdkCampaignGeo.

  1. Defaults Values
  • Bid Type = CPM
  • Pace evenly = true
  • campaign goal type = Engagement
  • campaign group budget type = cost
  • campaign group revenue type = CPM
import {
  initStackAdaptSDK,
  createCreativeByURL,
  ResourceType,
  createSimpleEngagementDisplayCampaign,
} from "@stackadapt/pa-typescript-sdk";

initStackAdaptSDK({
  token: MY_TOKEN,
  env: "SANDBOX",
});

// optional depending if creative ID already exist or if using JS tag creatives
const creative = await createCreativeByURL({
  resourceType: ResourceType.Image,
  url: IMAGE_PNG_300x600_URL,
});

const geo = {
  type: "location", // available types: geoRadius, politicalDistricts, location, zipTargeting, zipTargetingAndLocation
  payload: { locationIds: ["1242619", "1242881"] }, // this will change based on type
};

try {
  /* returns a campaign GQL fragment containing trivial campaign data: 
      campaign {
        id
        name
        ads {
          ...
        }
        advertiser {
          id
          name
        }
        campaignGroup {
          id
          name
        }
        campaignStatus {
          state
          status
        }
        createdAt
        updatedAt
        flights {
          ...
        }
        geo {
          ...
        }
        isArchived
        isDraft
      }
  */
  const campaignResult = await createSimpleEngagementDisplayCampaign({
    ads: [
      {
        name: "SDK Test Ad",
        brandname: "SDK Test",
        clickUrl: "https://www.stackadapt.com",
        creative: { creativeId: creative.id },
      },
    ],
    advertiser: { name: "Test Advertiser" },
    flightStartTime: "2025-01-02T05:00:00",
    flightEndTime: "2026-01-02T05:00:00",
    flightLifetimeBudget: 50000.5,
    bidAmount: 3,
    name: "Test Simple Engagement Display Campaign",
    isDraft: true,
    geo,
  });
} catch (e) {
  throw new Error(`Error creating campaign: ${e}`);
}