Run Authenticated Sessions
Retrieve a connected user session and execute custom automation scripts
Users must first be connected to Anon to save their user sessions.
Then, the user sessions will be available to run automation scripts on.
Anon makes it easy to run automation scripts on authenticated user sessions. Once a user has connected their account to your application, you can retrieve their session and execute custom logic as if you were the user interacting with the target website.
Here’s how to run authenticated user sessions with Anon:
You can get started with a Typescript example here.
Add Anon’s Typescript SDK
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.
To get started, add the SDK to your project’s dependencies.
# If you're using npm
npm install '@anon/sdk-typescript@^0.6.0'
npx playwright install
# If you're using yarn
yarn add '@anon/sdk-typescript@^0.6.0'
yarn install --update-checksums
yarn add @playwright/test
Execute an automation script
Now, you can import and configure the Typescript SDK Client
to start using sessions.
Use environment variables to pass secrets such as api_key
into your code.
Example Usage
import {
Client,
setupAnonBrowserWithContext,
executeRuntimeScript,
} from "@anon/sdk-typescript";
import { Page } from "playwright";
import "dotenv/config";
// this is the "sub" field of your user's JWT
const APP_USER_ID = process.env.ANON_APP_USER_ID!;
// create a server SdkClient and use its api_key
// for testing, can alternately use an admin member's api_key
const API_KEY = process.env.ANON_API_KEY!;
// "sandbox" or "prod", based on your credentials
const CLIENT_ENV = process.env.ANON_ENV!;
// check out Anon's 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
const APP = "instagram";
const account = {
app: APP,
userId: APP_USER_ID,
};
const client = new Client({
environment: CLIENT_ENV,
apiKey: API_KEY,
});
const main = async () => {
console.log(`Requesting ${account.app} session for appUserId ${account.userId}`);
const { browserContext } = await setupAnonBrowserWithContext(
client,
account,
{ type: "local", input: { headless: false } },
);
await executeRuntimeScript({
client,
account,
target: { browserContext: browserContext },
initialUrl: "https://instagram.com",
run: async (page: Page) => {
// enter your code here!
// here's an example of going to the messaging page
await page.goto("https://www.instagram.com/direct/inbox/")
await page.mainFrame().waitForLoadState();
await page.waitForTimeout(10000);
},
});
}
main();
This sample will setup a browser context with the user’s session and execute a script that navigates to Instagram’s messaging page.
From there, you can interact with the page as if you were the user. Send a message, read from the inbox, etc.
Next Steps
After integrating Anon, keep testing Playwright scripts with your desired use cases until you’re satisfied.
To use Anon in your production environment, please refer to this FAQ.
Was this page helpful?