Apify API client for JavaScript 0.5.2

ApifyClient

new ApifyClient(optionsopt)

Basic usage of ApifyClient:

const ApifyClient = require('apify-client');

const apifyClient = new ApifyClient({
  userId: 'jklnDMNKLekk',
  token: 'SNjkeiuoeD443lpod68dk',
});
Parameters:
  • options ( Object ) <optional> - Global options for ApifyClient. You can globally configure here any method option from any namespace. For example if you are working with just one crawler then you can preset it's crawlerId here instead of passing it to each crawler's method.
    • userId ( String ) <optional> - Your user ID at apify.com
    • token ( String ) <optional> - Your API token at apify.com
    • expBackOffMillis ( Number ) <optional> - Wait time in milliseconds before repeating request to Apify API in a case of server or rate limit error Defaults to 500.
    • expBackOffMaxRepeats ( Number ) <optional> - Maximum number of repeats in a case of error Defaults to 8.

Methods (2)

getOptions() → {Object}

Returns options of ApifyClient instance.

Returns:
  • ( Object ) - See ApifyClient constructor options
  • setOptions(options)

    Overrides options of ApifyClient instance.

    Parameters:

    ApifyClient.acts

    Basic usage

    Every method can be used as either promise or with callback. If your Node version supports await/async then you can await promise result.

    const ApifyClient = require('apify-client');
    
    const apifyClient = new ApifyClient({
     userId: 'jklnDMNKLekk',
     token: 'SNjkeiuoeD443lpod68dk',
    });
    
    // Awaited promise
    try {
         const crawler = await apifyClient.acts.listActs({});
         // Do something acts list ...
    } catch (err) {
         // Do something with error ...
    }
    
    // Promise
    apifyClient.acts.listActs({})
    .then((actsList) => {
         // Do something actsList ...
    })
    .catch((err) => {
         // Do something with error ...
    });
    
    // Callback
    apifyClient.acts.listActs({}, (err, actsList) => {
         // Do something with error or actsList ...
    });

    Methods (18)

    abortBuild(options, callbackopt) → {ActBuild}

    Abort act build.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • buildId ( String ) - Unique build ID
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( ActBuild )
  • abortRun(options, callbackopt) → {ActRun}

    Abort act run.

    Parameters:
    • options ( Object )
      • actId ( String ) - Unique act ID
      • runId ( String ) - Unique run ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( ActRun )
  • buildAct(options, callbackopt) → {ActBuild}

    Builds given act and returns object of that build.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • version ( String ) - Version of the act to build.
      • betaPackages ( Boolean ) <optional> - If true, the Docker container will be rebuild using layer cache. This is to enable quick rebuild during development.
      • useCache ( Boolean ) <optional> - If true, Docker build uses beta versions of 'apify-client' and 'apify' NPM packages, to test new features.
      • tag ( String ) <optional> - Tag that is applied to the build on success. It enables callers of acts to specify which version of act to run. betaPackages useCache tag
      • waitForFinish ( Number ) <optional> - Number of seconds to wait for act to finish. Maximum value is 120s. If act doesn't finish in time then act run in RUNNING state is returned.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( ActBuild )
  • createAct(options, callbackopt) → {Act}

    Creates a new act.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • act ( Object )
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Act )
  • createActVersion(options) → {ActVersion}

    Creates an act version.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • actVersion ( Object ) - Act version
    Returns:
  • ( ActVersion )
  • deleteAct(options, callbackopt)

    Deletes act.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
    • callback ( function ) <optional> - Callback function

    deleteActVersion(options)

    Deletes an act version.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • versionNumber ( String ) - Version number of act version
    Returns:

    getAct(options, callbackopt) → {Act}

    Gets act object.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Act )
  • getActVersion(options) → {ActVersion}

    Gets an act version.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • versionNumber ( String ) - Version number of act version
    Returns:
  • ( ActVersion )
  • getBuild(options, callbackopt) → {ActBuild}

    Gets act build.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • buildId ( String ) - Unique build ID
      • waitForFinish ( Number ) <optional> - Number of seconds to wait for act to finish. Maximum value is 120s. If act doesn't finish in time then act run in RUNNING state is returned.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( ActBuild )
  • getRun(options, callbackopt) → {ActRun}

    Gets act run.

    Parameters:
    • options ( Object )
      • actId ( String ) - Unique act ID
      • runId ( String ) - Unique run ID
      • token ( String ) <optional> - Your API token at apify.com
      • waitForFinish ( Number ) <optional> - Number of seconds to wait for act to finish. Maximum value is 120s. If act doesn't finish in time then act run in RUNNING state is returned.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( ActRun )
  • listActs(options, callbackopt) → {PaginationList}

    By default, the objects are sorted by the createdAt field in ascending order, therefore you can use pagination to incrementally fetch all acts while new ones are still being created. To sort them in descending order, use desc: true parameter. The endpoint supports pagination using limit and offset parameters and it will not return more than 1000 array elements.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Boolean ) <optional> - If true then the objects are sorted by the createdAt field in descending order.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • listActVersions(options) → {PaginationList}

    Gets the list of versions of a specific act.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
    Returns:
  • ( PaginationList )
  • listBuilds(options, callbackopt) → {PaginationList}

    Gets list of act builds.

    By default, the objects are sorted by the startedAt field in ascending order, therefore you can use pagination to incrementally fetch all builds while new ones are still being created. To sort them in descending order, use desc: true parameter.

    The endpoint supports pagination using limit and offset parameters and it will not return more than 1000 array elements.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Boolean ) <optional> - If true then the objects are sorted by the createdAt field in descending order.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • listRuns(options, callbackopt) → {PaginationList}

    Gets list of act runs.

    By default, the objects are sorted by the startedAt field in ascending order, therefore you can use pagination to incrementally fetch all builds while new ones are still being created. To sort them in descending order, use desc: true parameter.

    The endpoint supports pagination using limit and offset parameters and it will not return more than 1000 array elements.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Boolean ) <optional> - If true then the objects are sorted by the createdAt field in descending order.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • runAct(options, callbackopt) → {ActRun}

    Runs the latest build of given act.

    Parameters:
    • options ( Object )
      • actId ( String ) - Unique act ID
      • token ( String ) <optional> - Your API token at apify.com
      • body ( String | Buffer ) <optional> - Act input, passed as HTTP POST payload
      • contentType ( String ) <optional> - Content type of act input e.g 'application/json'
      • waitForFinish ( Number ) <optional> - Number of seconds to wait for act to finish. Maximum value is 120s. If act doesn't finish in time then act run in RUNNING state is returned.
      • timeout ( Number ) <optional> - Timeout for the act run in seconds. Zero value means there is no timeout.
      • memory ( Number ) <optional> - Amount of memory allocated for the act run, in megabytes.
      • build ( String ) <optional> - Tag or number of the build to run (e.g. latest or 1.2.34).
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( ActRun )
  • updateAct(options, callbackopt) → {Act}

    Updates act.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • act ( Object )
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Act )
  • updateActVersion(options) → {ActVersion}

    Updates an act version.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • actId ( String ) - Unique act ID
      • versionNumber ( String ) - Version number of act version
      • actVersion ( Object ) - Act version
    Returns:
  • ( ActVersion )
  • ApifyClient.crawlers

    Basic usage

    const ApifyClient = require('apify-client');
    
    const apifyClient = new ApifyClient({
           userId: 'RWnGtczasdwP63Mak',
           token: 'f5J7XsdaKDyRywwuGGo9',
    });
    
    const crawlerSettings = {
         customId: 'Test',
         startUrls: [ {key: 'test', value: 'http://example.com/' } ],
         pageFunction: `
             function pageFunction(context) {
                 // called on every page the crawler visits, use it to extract data from it
                 var $ = context.jQuery;
                 var result = {
                     title: $('title').text();
                 };
                 return result;
             }
         `,
         injectJQuery: true,
    };
    
    const crawler = await apifyClient.crawlers.createCrawler({ settings: crawlerSettings });
    const execution = await apifyClient.crawlers.startExecution({ crawlerId: crawler._id, wait: 5 });
    const results = await apifyClient.crawlers.getExecutionResults({ executionId: execution._id });
    console.log(results.items[0].pageFunctionResult) // { title: 'Example Domain' }

    Every method can be used as either promise or with callback. If your Node version supports await/async then you can await promise result.

    const options = { crawlerId: 'DNjkhrkjnri' };
    // Awaited promise
    try {
         const crawler = await apifyClient.crawlers.getCrawlerSettings(options);
         // Do something crawler ...
    } catch (err) {
         // Do something with error ...
    }
    // Promise
    apifyClient.crawlers.getCrawlerSettings(options)
    .then((crawler) => {
         // Do something crawler ...
    })
    .catch((err) => {
         // Do something with error ...
    });
    // Callback
    apifyClient.crawlers.getCrawlerSettings(options, (err, crawler) => {
         // Do something with error and crawler ...
    });

    Methods (12)

    createCrawler(options, callbackopt) → {CrawlerSettings}

    Creates a new crawler.

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • settings ( Object ) - Crawler settings, customId is required. See Crawler">www.apify.com/docs/crawler|Crawler documentation for detailed description of crawler settings. Unknown properties in the object are silently ignored.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( CrawlerSettings )
  • deleteCrawler(options, callbackopt)

    Deletes a specific crawler.

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    • options.crawlerId ( String ) - Crawler ID or crawler custom ID

    getCrawlerSettings(options, callbackopt) → {CrawlerSettings}

    Gets full details and settings of a specific crawler.

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • crawlerId ( String ) - Crawler ID or crawler custom ID
      • executionId ( String ) <optional> - Crawler execution ID for which the crawler settings should be returned.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( CrawlerSettings )
  • getExecutionDetails(options, callbackopt) → {Execution}

    Gets details of a single crawler execution.

    Parameters:
    • options ( Object )
      • executionId ( String ) - Execution ID
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Execution )
  • getExecutionResults(options, callbackopt) → {PaginationList}

    Gets results of a specific execution.

    Parameters:
    • options ( Object )
      • executionId ( String ) - Execution ID
      • format ( String ) <optional> - Format of the results, possible values are: json, jsonl, csv, html, xml and rss. Defaults to 'json'.
      • simplified ( Number ) <optional> - If 1 then the results will be returned in a simplified form without crawling metadata.
      • offset ( Number ) <optional> - Number of Request objects that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of Request objects to return. Defaults to 100000.
      • desc ( Number ) <optional> - By default, results are returned in the same order as they were stored in database. To reverse the order, set this parameter to 1.
      • attachment ( Number ) <optional> - If 1 then the response will define the Content-Disposition: attachment header, forcing a web browser to download the file rather than to display it. By default this header is not present.
      • delimiter ( String ) <optional> - A delimiter character for CSV files, only used if format=csv. You might need to URL-encode the character (e.g. use %09 for tab or %3B for semicolon). Defaults to ','.
      • bom ( Number ) <optional> - All responses are encoded in UTF-8 encoding. By default, the csv files are prefixed with the UTF-8 Byte Order Mark (BOM), while json, jsonl, xml, html and rss files are not. If you want to override this default behavior, specify bom=1 query parameter to include the BOM or bom=0 to skip it.
      • xmlRoot ( String ) <optional> - Overrides default root element name of xml output. By default the root element is results.
      • xmlRow ( String ) <optional> - Overrides default element name that wraps each page or page function result object in xml output. By default the element name is page or result based on value of simplified parameter.
      • hideUrl ( Number ) <optional> - If set to 1 then url field will not be added to each page function result object. By default each page function result object contains url field.
      • skipFailedPages ( Number ) <optional> - If set to 1 then pages with non-empty errorInfo property are skipped from the output and the errorInfo property is hidden. Note that the skipped pages are still counted in the pagination.
      • skipHeaderRow ( Number ) <optional> - If set to 1 then header row in csv format is skipped.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • getLastExecution(options, callbackopt) → {Execution}

    Gets information about the last execution of a specific crawler.

    Optionally, you can use status parameter to only get the last execution with a specific status.

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • crawlerId ( String ) - Crawler ID or crawler custom ID
      • status ( String ) <optional> - Filter for the execution status.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Execution )
  • getLastExecutionResults(options, callbackopt) → {PaginationList}

    Gets results of a last execution.

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • crawlerId ( String ) - Crawler ID or crawler custom ID
      • status ( String ) - Filter for the execution status. This field is mandatory, it must have on of the following values: RUNNING, SUCCEEDED, STOPPED, TIMEOUT or FAILED.
      • format ( String ) <optional> - Format of the results, possible values are: json, jsonl, csv, html, xml and rss. Defaults to 'json'.
      • simplified ( Number ) <optional> - If 1 then the results will be returned in a simplified form without crawling metadata.
      • offset ( Number ) <optional> - Number of Request objects that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of Request objects to return. Defaults to 100000.
      • desc ( Number ) <optional> - By default, results are returned in the same order as they were stored in database. To reverse the order, set this parameter to 1.
      • attachment ( Number ) <optional> - If 1 then the response will define the Content-Disposition: attachment header, forcing a web browser to download the file rather than to display it. By default this header is not present.
      • delimiter ( String ) <optional> - A delimiter character for CSV files, only used if format=csv. You might need to URL-encode the character (e.g. use %09 for tab or %3B for semicolon). Defaults to ','.
      • bom ( Number ) <optional> - All responses are encoded in UTF-8 encoding. By default, the csv files are prefixed with the UTF-8 Byte Order Mark (BOM), while json, jsonl, xml, html and rss files are not. If you want to override this default behavior, specify bom=1 query parameter to include the BOM or bom=0 to skip it.
      • xmlRoot ( String ) <optional> - Overrides default root element name of xml output. By default the root element is results.
      • xmlRow ( String ) <optional> - Overrides default element name that wraps each page or page function result object in xml output. By default the element name is page or result based on value of simplified parameter.
      • hideUrl ( Number ) <optional> - If set to 1 then url field will not be added to each page function result object. By default each page function result object contains url field.
      • skipFailedPages ( Number ) <optional> - If set to 1 then pages with non-empty errorInfo property are skipped from the output and the errorInfo property is hidden. Note that the skipped pages are still counted in the pagination.
      • skipHeaderRow ( Number ) <optional> - If set to 1 then header row in csv format is skipped.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • getListOfExecutions(options, callbackopt) → {PaginationList}

    Gets a list of executions of a specific crawler.

    Optionally, you can use status parameter to filter the list to only contain executions with a specific status (for example, status 'RUNNING' will only return executions that are still running).

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • crawlerId ( String ) - Crawler ID or crawler custom ID
      • status ( String ) <optional> - Filter for the execution status.
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Number ) <optional> - If 1 then the executions are sorted by the startedAt field in descending order.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • listCrawlers(options, callbackopt) → {PaginationList}

    By default, the objects are sorted by the createdAt field in ascending order, therefore you can use pagination to incrementally fetch all crawlers while new ones are still being created. To sort them in descending order, use desc: 1 parameter.

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Number ) <optional> - If 1 then the crawlers are sorted by the createdAt field in descending order.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • startExecution(options, callbackopt) → {Execution}

    Starts execution of a specific crawler.

    // Start execution and overwrite crawler settings
    const execution = await apifyClient.crawlers.startExecution({
      crawlerId: 'v6hb9olk86gfd8',
      settings: {
          startUrls: [
              {
                  key: "START",
                  value: 'http://example.com'
              }
          ]
      }
    });
    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • crawlerId ( String ) - Crawler ID or crawler custom ID
      • tag ( String ) <optional> - Custom tag for the execution. It cannot be longer than 64 characters.
      • wait ( Number ) <optional> - The maximum number of seconds the server waits for the execution to finish. Defaults to 0.
      • settings ( Object ) <optional> - Overwrites crawler settings for execution.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Execution )
  • stopExecution(options, callbackopt) → {Execution}

    Stops a specific crawler execution.

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • executionId ( String ) - Execution ID
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Execution )
  • updateCrawler(options, callbackopt) → {CrawlerSettings}

    Updates a specific crawler.

    Parameters:
    • options ( Object )
      • userId ( String ) - Your user ID at apify.com
      • token ( String ) - Your API token at apify.com
      • crawlerId ( String ) - Crawler ID or crawler custom ID
      • settings ( Object ) - Crawler settings, customId is required. See Crawler">www.apify.com/docs/crawler|Crawler documentation for detailed description of crawler settings. Unknown properties in the object are silently ignored.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( CrawlerSettings )
  • ApifyClient.datasets

    Basic usage

    const ApifyClient = require('apify-client');
    
    const apifyClient = new ApifyClient({
           userId: 'RWnGtczasdwP63Mak',
           token: 'f5J7XsdaKDyRywwuGGo9',
    });
    const datasets = apifyClient.datasets;
    
    // Get dataset with name 'my-dataset' and set it as default
    // to be used in following commands.
    const dataset = await datasets.getOrCreateDataset({
        datasetName: 'my-dataset',
    });
    apifyClient.setOptions({ datasetId: dataset.id });
    
    // Save some object and array of objects to dataset.
    await datasets.putItems({
         data: { foo: 'bar' }
    });
    await datasets.putItems({
         data: [{ foo: 'hotel' }, { foo: 'restaurant' }],
    });
    
    // Get items from dataset and delete it.
    const paginationList = await datasets.getItems();
    const items = paginationList.items;
    await datasets.deleteDataset();

    Every method can be used as either promise or with callback. If your Node version supports await/async then you can await promise result.

    // Awaited promise
    try {
         const items = await datasets.getItems();
         // Do something with the items ...
    } catch (err) {
         // Do something with error ...
    }
    
    // Promise
    datasets.getItems()
    .then((paginationList) => {
         console.log(paginationList.items)
         // Do something with items ...
    })
    .catch((err) => {
         // Do something with error ...
    });
    
    // Callback
    datasets.getItems((err, paginationList) => {
         console.log(paginationList.items)
         // Do something with error or items ...
    });

    Methods (6)

    deleteDataset(options, callbackopt) → {*}

    Deletes given dataset.

    Parameters:
    • options ( Object )
      • datasetId ( String ) - Unique dataset ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( * )
  • getDataset(options, callbackopt) → {Dataset}

    Returns given dataset.

    Parameters:
    • options ( Object )
      • datasetId ( String ) - Unique dataset ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Dataset )
  • getItems(options, callbackopt) → {PaginationList}

    Returns items in the dataset based on the provided parameters

    Parameters:
    • options ( Object )
      • datasetId ( String ) - Unique dataset ID
      • format ( String ) <optional> - Format of the items, possible values are: json, csv, xlsx, html, xml and rss. Defaults to 'json'.
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 100000.
      • desc ( Number ) <optional> - If true then the objects are sorted by createdAt in descending order. Otherwise they are sorted in ascending order.
      • fields ( Array ) <optional> - An array of field names that will be included in the result. If omitted, all fields are included in the results.
      • unwind ( String ) <optional> - Specifies a name of the field in the result objects that will be used to unwind the resulting objects. By default, the results are returned as they are.
      • disableBodyParser ( Boolean ) <optional> - If true then response from API will not be parsed
      • attachment ( Number ) <optional> - If true then the response will define the Content-Disposition: attachment HTTP header, forcing a web browser to download the file rather than to display it. By default, this header is not present.
      • delimiter ( String ) <optional> - A delimiter character for CSV files, only used if format is csv. You might need to URL-encode the character (e.g. use %09 for tab or %3B for semicolon). Defaults to ','.
      • bom ( Number ) <optional> - All responses are encoded in UTF-8 encoding. By default, the CSV files are prefixed with the UTF-8 Byte Order Mark (BOM), while JSON, JSONL, XML, HTML and RSS files are not. If you want to override this default behavior, set bom option to true to include the BOM, or set bom to false to skip it.
      • xmlRoot ( String ) <optional> - Overrides the default root element name of the XML output. By default, the root element is results.
      • xmlRow ( String ) <optional> - Overrides the default element name that wraps each page or page function result object in XML output. By default, the element name is page or result, depending on the value of the simplified option.
      • skipHeaderRow ( Boolean ) <optional> - If set to 1 then header row in csv format is skipped.
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • getOrCreateDataset(options, callbackopt) → {Dataset}

    Creates dataset of given name and returns it's object. If data with given name already exists then returns it's object.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • datasetName ( String ) - Custom unique name to easily identify the dataset in the future.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Dataset )
  • listDatasets(options, callbackopt) → {PaginationList}

    Returns a list of datasets owned by a user.

    By default, the objects are sorted by the createdAt field in ascending order, therefore you can use pagination to incrementally fetch all datasets while new ones are still being created. To sort them in descending order, use desc: true option. The endpoint supports pagination using limit and offset parameters and it will not return more than 1000 array elements.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Boolean ) <optional> - If true then the objects are sorted by the startedAt field in descending order.
      • unnamed ( Boolean ) <optional> - If true then also unnamed stores will be returned. By default only named stores are returned.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • putItems(options, callbackopt) → {*}

    Saves the object or an array of objects into dataset.

    Parameters:
    • options ( Object )
      • datasetId ( String ) - Unique dataset ID
      • data ( Object | Array | String ) - Object, Array of objects or a String. String must be a valid JSON. Arrays and Objects must be JSON.stringifiable.
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( * )
  • ApifyClient.keyValueStores

    Basic usage

    const ApifyClient = require('apify-client');
    
    const apifyClient = new ApifyClient({
           userId: 'RWnGtczasdwP63Mak',
           token: 'f5J7XsdaKDyRywwuGGo9',
    });
    const keyValueStores = apifyClient.keyValueStores;
    
    const store = await keyValueStores.getOrCreateStore({ storeName: 'my-store' });
    apifyClient.setOptions({ storeId: store.id });
    await keyValueStores.putRecord({
         key: 'foo',
         body: 'bar',
         contentType: 'text/plain; charset=utf-8',
    });
    const record = await keyValueStores.getRecord({ key: 'foo' });
    const keys = await keyValueStores.getRecordsKeys();
    await keyValueStores.deleteRecord({ key: 'foo' });

    Every method can be used as either promise or with callback. If your Node version supports await/async then you can await promise result.

    // Awaited promise
    try {
         const record = await keyValueStores.getRecord({ key: 'foo' });
         // Do something record ...
    } catch (err) {
         // Do something with error ...
    }
    
    // Promise
    keyValueStores.getRecord({ key: 'foo' })
    .then((RECORD) => {
         // Do something record ...
    })
    .catch((err) => {
         // Do something with error ...
    });
    
    // Callback
    keyValueStores.getRecord({ key: 'foo' }, (err, record) => {
         // Do something with error or record ...
    });

    Methods (8)

    deleteRecord(options, callbackopt)

    Deletes given record.

    Parameters:
    • options ( Object )
      • storeId ( String ) - Unique store ID
      • key ( String ) - Key of the record
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function

    deleteStore(options, callbackopt) → {*}

    Deletes key-value store.

    Parameters:
    • options ( Object )
      • storeId ( String ) - Unique store ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( * )
  • getOrCreateStore(options, callbackopt) → {KeyValueStore}

    Creates store of given name and returns it's object. If store with given name already exists then returns it's object.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • storeName ( String ) - Custom unique name to easily identify the store in the future.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( KeyValueStore )
  • getRecord(options, callbackopt) → {KeyValueStoreRecord}

    Gets value stored in the key-value store under the given key.

    Parameters:
    • options ( Object )
      • storeId ( String ) - Unique store ID
      • key ( String ) - Key of the record
      • disableBodyParser ( Boolean ) <optional> - It true, it doesn't parse record's body based on content type.
      • disableRedirect ( Boolean ) <optional> - API by default redirects user to signed record url for faster download. If disableRedirect=1 is set then API returns the record value directly.
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( KeyValueStoreRecord )
  • getStore(options, callbackopt) → {KeyValueStore}

    Gets key-value store.

    Parameters:
    • options ( Object )
      • storeId ( String ) - Unique store ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( KeyValueStore )
  • listKeys(options, callbackopt) → {PaginationList}

    Returns an array containing objects representing keys in given store.

    You can paginated using exclusiveStartKey and limit parameters.

    Parameters:
    • options ( Object )
      • storeId ( String ) - Unique store ID
      • exclusiveStartKey ( String ) <optional> - All keys up to this one (including) are skipped from the result.
      • limit ( Number ) <optional> - Number of keys to be returned. Maximum value is 1000
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • listStores(options, callbackopt) → {PaginationList}

    Gets list of key-value stores.

    By default, the objects are sorted by the createdAt field in ascending order, therefore you can use pagination to incrementally fetch all stores while new ones are still being created. To sort them in descending order, use desc: true parameter. The endpoint supports pagination using limit and offset parameters and it will not return more than 1000 array elements.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Boolean ) <optional> - If true then the objects are sorted by the startedAt field in descending order.
      • unnamed ( Boolean ) <optional> - If true then also unnamed stores will be returned. By default only named stores are returned.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • putRecord(options, callbackopt) → {*}

    Saves the record into key-value store.

    Parameters:
    • options ( Object )
      • storeId ( String ) - Unique store ID
      • key ( String ) - Key of the record
      • contentType ( String ) - Content type of body
      • body ( string | Buffer ) - Body in string or Buffer
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( * )
  • ApifyClient.logs

    Logs

    Methods (1)

    getLog(options, callbackopt) → {Promise.<string>|null}

    Parameters:
    • options ( Object )
      • logId ( String ) - ID of the log which is either ID of the act build or ID of the act run.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Promise.<string> | null )
  • ApifyClient.requestQueues

    Basic usage

    const ApifyClient = require('apify-client');
    
    const apifyClient = new ApifyClient({
           userId: 'RWnGtczasdwP63Mak',
           token: 'f5J7XsdaKDyRywwuGGo9',
    });
    const requestQueues = apifyClient.requestQueues;
    
    // Get request queue with name 'my-queue' and set it as default
    // to be used in following commands.
    const queue = await requestQueues.getOrCreateQueue({
        queueName: 'my-queue',
    });
    apifyClient.setOptions({ queueId: queue.id });
    
    // Add requests to queue.
    await requestQueues.addRequest({ url: 'http://example.com', uniqueKey: 'http://example.com' });
    await requestQueues.addRequest({ url: 'http://example.com/a/b', uniqueKey: 'http://example.com/a/b' });
    
    // Fetch unhandled requets from queue.
    const [request1, request2] = await requestQueues.queryQueueHead();
    
    // Mark request as handled.
    request1.handledAt = new Date();
    await requestQueues.updateRequest(request1);

    Every method can be used as either promise or with callback. If your Node version supports await/async then you can await promise result.

    // Awaited promise
    try {
         const queue = await requestQueues.getQueue(queueId);
         // Do something with the queue ...
    } catch (err) {
         // Do something with error ...
    }
    
    // Promise
    requestQueues.getQueue(queueId)
    .then((queue) => {
         // Do something with queue ...
    })
    .catch((err) => {
         // Do something with error ...
    });
    
    // Callback
    requestQueues.getQueue(queueId, (err, queue) => {
         // Do something with error or queue ...
    });

    Methods (9)

    addRequest(options, callbackopt) → {RequestOperationInfo}

    Adds request to the queue. If request is already in the queue then returns info about existing request.

    Parameters:
    • options ( Object )
      • queueId ( String ) - Unique queue ID
      • request ( Object ) - Request object
      • forefront ( Boolean ) <optional> - If yes then request will be enqueued to the begining of the queue and to the end of the queue otherwise.
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( RequestOperationInfo )
  • deleteQueue(options, callbackopt) → {*}

    Deletes request queue.

    Parameters:
    • options ( Object )
      • queueId ( String ) - Unique queue ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( * )
  • deleteRequest(options, callbackopt) → {*}

    Deletes request from queue.

    Parameters:
    • options ( Object )
      • queueId ( String ) - Unique queue ID
      • requestId ( String ) - Unique request ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( * )
  • getHead(options, callbackopt) → {QueueHead}

    Returns given number of the first unhandled requests in he queue.

    Parameters:
    • options ( Object )
      • queueId ( String ) - Unique queue ID
      • limit ( Number ) - Maximum number of the items to be returned.
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( QueueHead )
  • getOrCreateQueue(options, callbackopt) → {RequestQueue}

    Creates request queue of given name and returns it's object. If queue with given name already exists then returns it's object.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • queueName ( String ) - Custom unique name to easily identify the queue in the future.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( RequestQueue )
  • getQueue(options, callbackopt) → {RequestQueue}

    Gets request queue.

    Parameters:
    • options ( Object )
      • queueId ( String ) - Unique queue ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( RequestQueue )
  • getRequest(options, callbackopt) → {Request}

    Gets request from the queue.

    Parameters:
    • options ( Object )
      • queueId ( String ) - Unique queue ID
      • requestId ( String ) - Unique request ID
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Request )
  • listQueues(options, callbackopt) → {PaginationList}

    Gets list of request queues.

    By default, the objects are sorted by the createdAt field in ascending order, therefore you can use pagination to incrementally fetch all queues while new ones are still being created. To sort them in descending order, use desc: true parameter. The endpoint supports pagination using limit and offset parameters and it will not return more than 1000 array elements.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Boolean ) <optional> - If true then the objects are sorted by the startedAt field in descending order.
      • unnamed ( Boolean ) <optional> - If true then also unnamed stores will be returned. By default only named stores are returned.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • updateRequest(options, callbackopt) → {RequestOperationInfo}

    Updates request in the queue.

    Parameters:
    • options ( Object )
      • queueId ( String ) - Unique queue ID
      • request ( Object ) - Request object
      • requestId ( String ) <optional> - Unique request ID
      • forefront ( Boolean ) <optional> - If yes then request will be enqueued to the begining of the queue and to the end of the queue otherwise.
      • token ( String ) <optional> - Your API token at apify.com
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( RequestOperationInfo )
  • ApifyClient.tasks

    Basic usage

    Every method can be used as either promise or with callback. If your Node version supports await/async then you can await promise result.

    const ApifyClient = require('apify-client');
    
    const apifyClient = new ApifyClient({
     userId: 'jklnDMNKLekk',
     token: 'SNjkeiuoeD443lpod68dk',
    });
    
    // Awaited promise
    try {
         const tasksList = await apifyClient.tasks.listTasks({});
         // Do something with the tasksList ...
    } catch (err) {
         // Do something with error ...
    }
    
    // Promise
    apifyClient.tasks.listTasks({})
    .then((tasksList) => {
         // Do something tasksList ...
    })
    .catch((err) => {
         // Do something with error ...
    });
    
    // Callback
    apifyClient.tasks.listTasks({}, (err, tasksList) => {
         // Do something with error or tasksList ...
    });

    Methods (7)

    createTask(options, callbackopt) → {Task}

    Creates a new task.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • task ( Object ) - Object containing configuration of the task
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Task )
  • deleteTask(options, callbackopt)

    Deletes task.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • taskId ( String ) - Unique task ID
    • callback ( function ) <optional> - Callback function

    getTask(options, callbackopt) → {Task}

    Gets task object.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • taskId ( String ) - Unique task ID
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Task )
  • listRuns(options, callbackopt) → {PaginationList}

    Gets list of task runs.

    By default, the objects are sorted by the startedAt field in ascending order, therefore you can use pagination to incrementally fetch all builds while new ones are still being created. To sort them in descending order, use desc: true parameter.

    The endpoint supports pagination using limit and offset parameters and it will not return more than 1000 array elements.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • taskId ( String ) - Unique task ID
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Boolean ) <optional> - If true then the objects are sorted by the createdAt field in descending order.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • listTasks(options, callbackopt) → {PaginationList}

    By default, the objects are sorted by the createdAt field in ascending order, therefore you can use pagination to incrementally fetch all tasks while new ones are still being created. To sort them in descending order, use desc: true parameter. The endpoint supports pagination using limit and offset parameters and it will not return more than 1000 array elements.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • offset ( Number ) <optional> - Number of array elements that should be skipped at the start. Defaults to 0.
      • limit ( Number ) <optional> - Maximum number of array elements to return. Defaults to 1000.
      • desc ( Boolean ) <optional> - If true then the objects are sorted by the createdAt field in descending order.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( PaginationList )
  • runTask(options, callbackopt) → {ActRun}

    Runs the given task.

    Parameters:
    • options ( Object )
      • taskId ( String ) - Unique task ID
      • token ( String ) <optional> - Your API token at apify.com
      • waitForFinish ( Number ) <optional> - Number of seconds to wait for task to finish. Maximum value is 120s. If task doesn't finish in time then task run in RUNNING state is returned.
      • body ( String ) <optional> - Actor input stringified as JSON, passed as HTTP POST payload
      • contentType ( String ) <optional> - Content type of act input e.g 'application/json'
      • timeout ( Number ) <optional> - Timeout for the act run in seconds. Zero value means there is no timeout.
      • memory ( Number ) <optional> - Amount of memory allocated for the act run, in megabytes.
      • build ( String ) <optional> - Tag or number of the build to run (e.g. latest or 1.2.34).
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( ActRun )
  • updateTask(options, callbackopt) → {Task}

    Updates task.

    Parameters:
    • options ( Object )
      • token ( String ) - Your API token at apify.com
      • taskId ( String ) - Unique task ID
      • task ( Object )
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( Task )
  • ApifyClient.users

    Users

    Methods (1)

    getUser(options, callbackopt) → {UserInfo}

    Returns private and public information about user account.

    Parameters:
    • options ( Object )
      • token ( String ) <optional> - If set, the function returns a private and public information for the user account, otherwise it only returns public information.
      • userId ( String ) <optional> - Desired user ID or username. By default it is 'me', which causes the function to return information about the current user. Defaults to 'me'.
    • callback ( function ) <optional> - Callback function
    Returns:
  • ( UserInfo )
  • Globals

    Type Definitions

    QueueHead

    Properties:
    • limit ( Number ) - Maximum number of items to be returned.
    • queueModifiedAt ( Date ) - Date of the last modification of the queue.
    • items ( Array ) - Array of objects containing id, url, method, uniqueKey and retryCount attributes.

    RequestOperationInfo

    Properties:
    • wasAlreadyPresent ( Boolean ) - Indicates if request was already present in the queue.
    • wasAlreadyHandled ( Boolean ) - Indicates if request was already marked as handled.
    • requestId ( String ) - The ID of the added request