Anon’s Runtime SDKs run in multiple contexts, and are designed to be as flexible as possible. This guide will walk you through the process of integrating the SDK into your backend, web apps, and/or mobile apps.

The core of the SDKs is a type-safe REST client, combined with a set of utility functions for handling sessions in various browser runtimes.

Integrating the Runtime SDK

To get started, add the SDK to your project’s dependencies.

These SDKs are not published to public package registries at this time, so please head here to get access. We’ll give you an access token that you can use to connect to Anon’s private package registries for NPM, PyPI, and more.

Create a .npmrc file in the same directory as your package.json and have it contain the following snippet. Replace NPM_TOKEN with your auth token received from Anon.

@anon:registry=https://npm.cloudsmith.io/anon/anon-sdk/
//npm.cloudsmith.io/anon/anon-sdk/:_authToken=NPM_TOKEN
@npm:registry=https://registry.npmjs.org/
# If you're using npm
npm install '@anon/sdk-typescript@0.3.0'

# If you're using yarn
yarn add '@anon/sdk-typescript@0.3.0'
yarn install --update-checksums
# the --update-checksums is necessary to support patched packages in the SDK.
# this will be removed in future versions!

# Install the playwright browser binaries if you do not have them already.
npx install playwright

Once you’ve added the SDK to your dependencies, you can import and configure the Anon Client to start using sessions.

You’ll need to have your API Key secret handy for this step.

/*
  Configure your SdkClient API_KEY by editing static values or loading them via environment variables.
*/
import {
  Client,
  setupAnonBrowserWithContext,
  executeRuntimeScript,
} from "@anon/sdk-typescript";  // example updated to @0.3.0
import { Page } from "playwright";

const account = {
  // check out our list of supported apps here: https://docs.anon.com/docs/getting-started/overview
  // this should align with a session you uploaded with the web-link example
  app: "instagram",
  // this is the "sub" field of your user's JWTs
  userId: process.env.ANON_APP_USER_ID!,
};

const client = new Client({
  environment: "sandbox",
  // create a server SdkClient and use its ApiKey
  // for testing, can alternately use an admin member's api key
  apiKey: process.env.ANON_API_KEY!,
});

console.log(`Requesting ${account.app} session for app user id ${account.userId}`);

const main = async () => {
  const { browserContext } = await setupAnonBrowserWithContext(
    client,
    account,
    { type: "sandbox", input: { headless: false } },
  );
  executeRuntimeScript({
    client,
    account,
    target: { browserContext: browserContext },
    initialUrl: "https://instagram.com",
    run: async (page: Page) => {
      // your code here!
      // here's an example of going to the messaging page
      await page.goto("https://instagram.com/");
      await page.mainFrame().waitForLoadState();

      await page.goto("https://www.instagram.com/direct/inbox/")
      await page.mainFrame().waitForLoadState();
      await page.waitForTimeout(10000);
    },
  });
}

main()

Use environment variables to pass your API Key secret into your Client configuration. Don’t store your secret as plaintext in your codebase.

That’s it! You’re ready to start using sessions.

Running Automations for your integration

The following format may be used for any of our supported apps.

import { runInBrowser } from '@anon/sdk-typescript';
import { Page } from 'playwright';

await runInBrowser({
    account: {
        app: "<appName>",
        userId: 'user_1234'
    },
    url: https://www.<appName>.com,
    headless: false,
    script: async ( page: Page ) => {
        // Now you're ready to perform actions on the user's behalf using a logged in playwright instance.
        // Confirm action
        await page.goto("https://www.<appName>.com");

        // Your automation code here!
    }
})