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 🙏

© 2025 – Pkg Stats / Ryan Hefner

segment-typescript-api

v0.6.10

Published

TypeScript API for consuming Segment.com profile, config, and HTTP tracking APIs

Downloads

9

Readme

Segment TypeScript API

Overview

Segment provides various REST APIs with which to interact. This package provides convenient TypeScript functions for calling these APIs. The APIs covered are:

  • The HTTP Tracking API. This is used for non web or mobile end-points wishing to send events to Segment.
  • The Config API. This is used for creating and configuring Workspaces in Segment. Note: This isn't 100% complete. PRs are welcome.
  • The Profile API. This is used for querying the identity graph that Segment's Personas feature provides

Important: If you plan on consuming the HTTP Tracking API via TypeScript, you must define the types SegmentTrackProtocol and SegmentEvents. This is used for strongly typing track calls. If no defined schema exists, simply setting:

declare type SegmentEvents = string
declare type SegmentTrackProtocol = any

in some .d.ts file will suffice. There are also optional SegmentIdentifyProtocol and SegmentIdentifyProtocol interfaces (for strongly typing identify and group calls). For more information see Segment TypeScript Definitions.

Install

Install via NPM:

npm i --save segment-typescript-api

or Yarn:

yarn install segment-typescript-api

Usage

Each API is divided into two files:

  • a _api module (e.g. config_api). This contains an easy-to-use API for calling each endpoint. This is probably the what you want to consume.
  • a _request module (e.g. config_request). This contains typing definitions that define the endpoints, and a generic function for calling the endpoint.

An example:

import * as config from 'segment-typescript-api/cjs/config_api'

let WORKSPACE_TOKEN = '123'
let WORKSPACE_SLUG = 'my-workspace'

config.getWorkspace(WORKSPACE_TOKEN, WORKSPACE_SLUG).then(workspace=>{
  console.log(workspace)
})

If you're using ESNext modules, import from the esm namespace:

import * as config from 'segment-typescript-api/esm/config_api'

Preview Function Usage (For commonjs)

let api = require('./cjs/config_api')

const ACCESS_TOKEN = '###' // This is a workspace access token (https://segment.com/docs/config-api/#sts=Access%20Tokens)
const WORK_ID = '###' // Workspace ID (found under Workspace Settings/General Settings/ID)
const SRC_FN_CODE = 'async function onRequest(r){let rr = await r.text(); console.log(rr)}' // This is the actual function code
const SRC_PAYLOAD = { // This is the test-payload to test the source function with
  body: {}, // The HTTP body
  headers: {}, // HTTP headers
  queryParameters: {} // HTTP URL query parameters
}
const DEST_FN_CODE = 'async function onTrack(event, settings) {console.log(event)}' // This is the actual function code
const DEST_PAYLOAD = { // This is the test-payload to test the destination function with. Check out the examples in any of the APIs: https://segment.com/docs/connections/spec/
  // This is a complete example. You would only need to populate the fields that your function would actually use (although some are required; see comments)
  anonymousId: '23adfd82-aa0f-45a7-a756-24f2a7a4c895', // EITHER ONE OF userId OR anonymousId ARE REQUIRED (OR BOTH)
  context: {
    library: {
      name: 'analytics.js',
      version: '2.11.1'
    },
    page: {
      path: '/academy/',
      referrer: '',
      search: '',
      title: 'Analytics Academy',
      url: 'https://segment.com/academy/'
    },
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36',
    ip: '108.0.78.21'
  },
  event: 'Course Clicked', // FOR TRACK-EVENTS, THIS IS A REQUIRED FIELD
  integrations: {},
  messageId: 'ajs-f8ca1e4de5024d9430b3928bd8ac6b96',
  properties: { // FOR TRACK-EVENTS, THIS IS A REQUIRED FIELD
    title: 'Intro to Analytics'
  },
  receivedAt: '2015-12-12T19:11:01.266Z',
  sentAt: '2015-12-12T19:11:01.169Z',
  timestamp: '2015-12-12T19:11:01.249Z',
  type: 'track', // REQUIRED FIELD; THIS SPECIFIES THE KIND OF EVENT. SEE API DOCS FOR MORE DETAILS: https://segment.com/docs/connections/spec/
  userId: 'AiUGstSDIg', // EITHER ONE OF userId OR anonymousId ARE REQUIRED (OR BOTH)
  originalTimestamp: '2015-12-12T19:11:01.152Z'
}

api.previewSrcFunction(ACCESS_TOKEN, WORK_ID, {
  function: {
    buildpack: 'boreal',
    code: SRC_FN_CODE
  }
}, { payload: SRC_PAYLOAD }).then(r => {
  console.log(r)
})

api.previewDestFunction(ACCESS_TOKEN, WORK_ID, {
  function: {
    buildpack: 'boreal',
    code: DEST_FN_CODE
  }
}, DEST_PAYLOAD).then(r => {
  console.log(r)
})

TODO

PRs are welcome :)

  • Finish the Config API
  • Document the API calls. Currently, the user has to reference the docs to understand what each call does. This info should be included as JSDocs.
  • Implement the Protocols Debug API