Mastra social media agent
An AI agent for social media research, built on a TypeScript-native framework.
src/main.ts
src/tools.ts
1// Apify SDK - toolkit for building Apify Actors (Read more at https://docs.apify.com/sdk/js/)2import { Actor, log } from 'apify';3
4import { createSocialMediaAgent } from './agents.js';5
6// this is ESM project, and as such, it requires you to specify extensions in your relative imports7// read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions8// note that we need to use `.js` even when inside TS files9// import { router } from './routes.js';10
11// Actor input schema12interface Input {13 query: string;14 modelName: string;15}16
17// The init() call configures the Actor to correctly work with the Apify-provided environment - mainly the storage infrastructure. It is necessary that every Actor performs an init() call.18await Actor.init();19
20/**21 * Actor code22 */23
24// Handle input25const {26 // The query default value is provided only for template testing purposes.27 // You can remove it.28 query,29 modelName,30} = (await Actor.getInput()) as Input;31if (!query) {32 throw new Error('An agent query is required.');33}34
35// Create the social media agent with tools36const agent = createSocialMediaAgent(modelName);37
38log.info(`Querying the agent with the following query: ${query}`);39
40// Query the agent and get the response41const response = await agent.generate([{ role: 'user', content: query }]);42
43log.info(`Agent response: ${response.text}`);44
45// Charge for the task completion46await Actor.charge({ eventName: 'task-completed' });47
48// Push results into the dataset49await Actor.pushData({50 query,51 response: response.text,52});53
54// Gracefully exit the Actor process. It's recommended to quit all Actors with an exit()55await Actor.exit();A template for Mastra projects in TypeScript for building AI agents with Apify Actors . The template provides a basic structure and an example agent that calls Actors as tools in a workflow.
An agent is created and given a set of tools to accomplish a task. The agent receives a query from the user and decides which tools to use and in what order to complete the task. In this case, the agent is provided with an Instagram Scraper Actor to scrape Instagram profile posts. The agent produces textual output, which is saved to a dataset.
Add or modify the agent tools in the src/tools.ts file, and make sure to include new tools in the agent tools list in src/agents.ts. Additionally, you can update the agent prompts in src/agents.ts. For more information, refer to the Mastra agent documentation and the Mastra tools documentation .
This template uses the Pay Per Event (PPE) monetization model, which provides flexible pricing based on defined events.
To charge users, define events in JSON format and save them on the Apify platform. Here is an example schema with the task-completed event:
[{"task-completed": {"eventTitle": "Task completed","eventDescription": "Cost per query answered.","eventPriceUsd": 0.1}}]
In the Actor, trigger the event with:
await Actor.charge({ eventName: 'task-completed' });
This approach allows you to programmatically charge users directly from your Actor, covering the costs of execution and related services, such as LLM input/output tokens.
To set up the PPE model for this Actor:
- Configure the OpenAI API key environment variable: provide your OpenAI API key to the
OPENAI_API_KEYin the Actor's Environment variables. - Configure Pay Per Event: establish the Pay Per Event pricing schema in the Actor's Monetization settings. First, set the Pricing model to
Pay per eventand add the schema. An example schema can be found in pay_per_event.json.
- Apify SDK for JavaScript - a toolkit for building Apify Actors and scrapers in JavaScript
- Input schema - define and easily validate a schema for your Actor's input
- Dataset - store structured data where each object stored has the same attributes
- Key-value store - store any kind of data, such as JSON documents, images, or text files
TypeScript Cheerio crawler
A fast HTTP crawler that extracts data from every page. Good for simple sites like blogs, news, or product listings, but it can't run client-side JavaScript.
TypeScript one-page scraper
Get data from one web page with Cheerio. The simplest way to start scraping.
TypeScript Puppeteer scraper
A headless Chrome scraper that renders JavaScript before extracting data. Good for social feeds, dashboards, or single-page apps.
TypeScript Playwright scraper
A Playwright-based browser scraper supporting multiple browsers and contexts.
TypeScript Camoufox scraper
A Firefox-based browser built to look like a real user and bypass bot protection.
Playwright test runner
An automated browser test runner with results you can access via API.