2

Need help to implement Apify webhook. It takes some time to complete a task. I want to add a Apify webhook which will run another task but not sure how to do that.

$.ajax({
  url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120',  
  method : 'POST',
  contentType: 'application/json',
  dataType: 'json',
  data : JSON.stringify ({
      "queries" : "Outreach link building"
  }),
  success:function(result) {
    console.log(result);
  } 
});

The webhook will then call the following task:

$.ajax({
   url : `https://api.apify.com/v2/datasets/${datasetId}/items?format=json`,  
   method : 'GET',
   contentType: 'application/json; charset=utf-8',
   success:function(response) {
     console.log(response); // Items from dataset
   } 

 });

Btw if I am wrong with the way I want to implement what I need, please let me know your suggestion.

Ben Jonson
  • 565
  • 7
  • 22
  • The webhook doesn't call any task. It simply sends a "notification" to your endpoint that the run has finished. You need to have some kind of server to listen for the webhook. – Lukáš Křivka Sep 10 '19 at 13:03
  • Understood. Can I get some code example? Say after running the actor, I will store the datasetId in my database. Once the actor build is completed, I want the hook sends a notification to an url of my app. Based on the notification I will update the status of the datasetId. – Ben Jonson Sep 10 '19 at 13:39
  • Wouldn't be then better to do the integration with Apify via your server and then communicate it to your front-end? – Lukáš Křivka Sep 11 '19 at 07:41
  • Ya, I will do it via my server. How will I know that the dataset is ready after running the actor? – Ben Jonson Sep 11 '19 at 08:57
  • There are items pushed to the dataset from the moment you start the run. So just poll it every second and display the updated data. – Lukáš Křivka Sep 11 '19 at 11:18

1 Answers1

1

If you don't want to use a server at all underneath your application and you want to manage everything on from the client-side (front-end) and the 5 minutes max delay for the synchronous run it too short for you, you can use polling. You simply call the run endpoint few times until it will return with the status succeeded.

But if you can you the waitForFinish, just do that and then send the second call to get the dataset in the

success:function(result) {
    console.log(result);
   // instead of this log, you need to put your second call here and use the `result.data.defaultDatasetId` as ID of the dataset
  } 

If you have to wait longer than 300 seconds, you will need to use polling. But I would really avoid that or don't use ajax with callbacks but more modern fetch.

$.ajax({
  url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120',  
  method : 'POST',
  contentType: 'application/json',
  dataType: 'json',
  data : JSON.stringify ({
      "queries" : "Outreach link building"
  }),
  success:function(result) {
    console.log(result);
    // Here instead of just logging, you get the run object back with its `status`. You also find an `id` there and you can periodically poll the run with this endpoint
https://apify.com/docs/api/v2#/reference/actors/run-object/get-run

  } 
Lukáš Křivka
  • 953
  • 6
  • 9
  • The app will store information in its database. Log is used here to test the result. – Ben Jonson Sep 10 '19 at 13:36
  • Like [this](https://stackoverflow.com/questions/57848672/how-to-call-apify-google-search-scrapper-task-using-jquery-ajax/57848885?noredirect=1#comment102125590_57848885)? If yes, I actually would like to have a better user experience. Rather than keeping the user waiting to see things happen, I would like calll the dataset programmatically when it is ready. Can I do that? – Ben Jonson Sep 10 '19 at 13:43
  • Also I have edited my question, would you please check and let me know if the code is right? – Ben Jonson Sep 10 '19 at 14:09
  • You can call the dataset in some interval and update the displayed data. – Lukáš Křivka Sep 11 '19 at 07:45