Call actor
This example demonstrates how to start an Apify actor using
Apify.call()
and how to call the Apify API using
Apify.client
.
The script gets a random weird word and its explanation from randomword.com
and sends it to your email using the apify/send-mail
actor.
To make the example work, you'll need an Apify account.
Go to the Account - Integrations page to obtain your API token
and set it to the APIFY_TOKEN
environment variable,
or run the script using the Apify CLI. If you deploy this actor to the Apify Cloud, you can do things like set
up a scheduler to run your actor early in the morning.
To see what other actors are available, visit the Apify Store.
To run this example on the Apify Platform, select the
Node.js 12 + Chrome on Debian (apify/actor-node-chrome)
base image on the Source tab when configuring the actor.
const { Actor } = require('apify');
const { launchPuppeteer } = require('crawlee');
Actor.main(async () => {
// Launch the web browser.
const browser = await launchPuppeteer();
console.log('Obtaining own email address...');
const apifyClient = Actor.newClient();
const user = await apifyClient.user().get();
// Load randomword.com and get a random word
console.log('Fetching a random word.');
const page = await browser.newPage();
await page.goto('https://randomword.com/');
const randomWord = await page.$eval('#shared_section', (el) => el.outerHTML);
// Send random word to your email. For that, you can use an actor we already
// have available on the platform under the name: apify/send-mail.
// The second parameter to the Apify.call() invocation is the actor's
// desired input. You can find the required input parameters by checking
// the actor's documentation page: https://apify.com/apify/send-mail
console.log(`Sending email to ${user.email}...`);
await Actor.call('apify/send-mail', {
to: user.email,
subject: 'Random Word',
html: `<h1>Random Word</h1>${randomWord}`,
});
console.log('Email sent. Good luck!');
// Close Browser
await browser.close();
});