Mastra agent
Example of how to use Mastra with Apify Actors to create a social media analysis agent.
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';
3import { createSocialMediaAgent } from './agents.js';
4
5// this is ESM project, and as such, it requires you to specify extensions in your relative imports
6// read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions
7// note that we need to use `.js` even when inside TS files
8// import { router } from './routes.js';
9
10// Actor input schema
11interface Input {
12 query: string;
13 modelName: string;
14}
15
16// The init() call configures the Actor for its environment. It's recommended to start every Actor with an init()
17await Actor.init();
18
19/**
20 * Actor code
21*/
22
23// Charge for Actor start
24await Actor.charge({ eventName: 'actor-start' });
25
26// Handle input
27const {
28 // The query default value is provided only for template testing purposes.
29 // You can remove it.
30 query,
31 modelName,
32} = await Actor.getInput() as Input;
33if (!query) {
34 throw new Error('An agent query is required.');
35}
36
37// Create the social media agent with tools
38const agent = createSocialMediaAgent(modelName);
39
40log.info(`Querying the agent with the following query: ${query}`);
41
42// Query the agent and get the response
43const response = await agent.generate([
44 { role: 'user', content: query },
45]);
46
47log.info(`Agent response: ${response.text}`);
48
49// Charge for the task completion
50await Actor.charge({ eventName: 'task-completed' });
51
52// Push results into the dataset
53await Actor.pushData({
54 query,
55 response: response.text,
56});
57
58// Gracefully exit the Actor process. It's recommended to quit all Actors with an exit()
59await Actor.exit();
TypeScript Mastra agent template
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.
How it works
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.
How to use
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.
Pay Per Event
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:
1[ 2 { 3 "task-completed": { 4 "eventTitle": "Task completed", 5 "eventDescription": "Cost per query answered.", 6 "eventPriceUsd": 0.1 7 } 8 } 9]
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_KEY
in 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 event
and add the schema. An example schema can be found in .actor/pay_per_event.json.
Included features
- 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
Resources
Scrape single page with provided URL with Axios and extract data from page's HTML with Cheerio.
A scraper example that uses Cheerio to parse HTML. It's fast, but it can't run the website's JavaScript or pass JS anti-scraping challenges.
Example of a Puppeteer and headless Chrome web scraper. Headless browsers render JavaScript and are harder to block, but they're slower than plain HTTP.
Web scraper example with Crawlee, Playwright and headless Chrome. Playwright is more modern, user-friendly and harder to block than Puppeteer.
Web scraper example with Crawlee, Playwright and headless Camoufox. Camoufox is a custom stealthy fork of Firefox. Try this template if you're facing anti-scraping challenges.
Example of using the Playwright Test project to run automated website tests in the cloud and display their results. Usable as an API.