You'll need

Step 1: Set up your actor on a local machine

First of all, you need to create a local environment for developing Apify actors using the Apify quick start repository. You can do that using the following commands in your terminal:

git clone https://github.com/Apifier/act-quick-start.git my-first-act
cd my-first-act
npm install
npm run test-local

If everything goes well, you should see "Input message: Hello world!" in the terminal. This means that you have successfully run your first actor on your local machine!

Step 2: Lauch Puppeteer in the actor

Now let's add Puppeteer to the actor. The source code of the actor is present in the  main.js file. To launch Puppeteer, just call Apify.launchPuppeteer() from the  Apify.main() function. It returns a promise resolving to a Puppeteer browser instance. For example:

const Apify = require('apify');

Apify.main(async () => {
    console.log('Launching Puppeteer...');
    const browser = await Apify.launchPuppeteer();

    const page = await browser.newPage();
    await page.goto('http://example.com');

    // Wait 30s to see Chromium works :)
    await new Promise(resolve => setTimeout(resolve, 30000));

    await browser.close();
});

The above code opens a Chromium browser for 30 seconds and loads http://example.com. This is useful to ensure that you can open the browser locally.

Step 3: Get input and set output of the actor

Now let's make the actor accept the URL to open as its input.

An actor can have its input and output, which is stored in the Apify key-value store, created automatically for each actor run. You can access the values from the store using the Apify.getValue() and  Apify.setValue() functions. To emulate the key-value store during local development, it is possible to use the APIFY_DEV_KEY_VALUE_STORE_DIR environment variable. The quick-start actor defines this environment variable in the package.json file as ./kv-store-dev. For more details, read here.

So let's open the file at ./kv-store-dev/INPUT and change it to the following JSON:

{
    "url": "http://example.com"
}

Then you can access the input URL from the actor by calling Apify.getValue('INPUT') . Similarly, the output of the actor is stored by calling  Apify.setValue('OUTPUT', ...) 

const Apify = require('apify');

Apify.main(async () => {
    const input = await Apify.getValue('INPUT');
    console.log('My input is:');
    console.dir(input);
    const { url } = input;

    console.log('Launching Puppeteer...');
    const browser = await Apify.launchPuppeteer();
    const page = await browser.newPage();
    await page.goto(url);
    const pageTitle = await page.title();
    await browser.close();

    const output = { pageTitle, url };
    console.log('My output is:');
    console.dir(output);
    await Apify.setValue('OUTPUT', output);
});

Now run your actor by entering the following command into your terminal:

npm run test-local

You should see the output of the actor stored in a JSON file at ./kv-store-dev/OUTPUT

Step 4: Set up the actor on the Apify platform

Now it's time to move your actor to the cloud 🚀. First of all, go to the Apify app and log in to your account. Then go to the "Actor" section in the left-hand menu and create a new actor by clicking the Create new button. Set the actor name and click the Save button.

Go to the Source tab where you can write your code. To keep the tutorial simple, choose Source type: Hosted source and just copy & paste the code from main.js into the Source code editor. The Base image needs to be Node.js 8 + Puppeteer on Debian, so that Puppeteer will be available. Click the Save button to save all your changes.

Step 5: Build the actor

Before you can run the actor, you need to build it by pressing the Build button.

Step 6: Run the actor

Now you're ready to run the actor in the Apify cloud. Go to the Console tab and fill out Body to set the input for the actor. For example:

{
    "url": "https://apify.com"
}

Then click the Run button.

You should see the actor running and also see its console output:

The actor can also be started via the Apify API. Go to the API tab to see the corresponding API endpoints.

And that's it, your first actor is ready!

Thanks for reading this tutorial, we hope you found it useful. If you're missing something, feel free to contact us in the comments section. 👍 

Did this answer your question?