@@ -13,7 +13,6 @@ type IApiRequestOptions = IRequestOptions & { uri?: string };
13
13
14
14
/**
15
15
* Make an API request to Apify
16
- *
17
16
*/
18
17
export async function apiRequest (
19
18
this : IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions ,
@@ -24,16 +23,22 @@ export async function apiRequest(
24
23
const query = qs || { } ;
25
24
const endpoint = `https://api.apify.com${ uri } ` ;
26
25
26
+ const headers : Record < string , string > = {
27
+ 'x-apify-integration-platform' : 'n8n' ,
28
+ 'x-apify-integration-app-id' : 'website-content-crawler-app' ,
29
+ } ;
30
+
31
+ if ( isUsedAsAiTool ( this . getNode ( ) . type ) ) {
32
+ headers [ 'x-apify-integration-ai-tool' ] = 'true' ;
33
+ }
34
+
27
35
const options : IRequestOptions = {
28
36
json : true ,
29
37
...rest ,
30
38
method,
31
39
qs : query ,
32
40
url : endpoint ,
33
- headers : {
34
- 'x-apify-integration-platform' : 'n8n' ,
35
- 'x-apify-integration-app-id' : 'website-content-crawler-app' ,
36
- } ,
41
+ headers,
37
42
} ;
38
43
39
44
if ( method === 'GET' ) {
@@ -42,6 +47,7 @@ export async function apiRequest(
42
47
43
48
try {
44
49
const authenticationMethod = this . getNodeParameter ( 'authentication' , 0 ) as string ;
50
+
45
51
try {
46
52
await this . getCredentials ( authenticationMethod ) ;
47
53
} catch {
@@ -53,10 +59,7 @@ export async function apiRequest(
53
59
54
60
return await this . helpers . requestWithAuthentication . call ( this , authenticationMethod , options ) ;
55
61
} catch ( error ) {
56
- /**
57
- * using `error instanceof NodeApiError` results in `false`
58
- * because it's thrown by a different instance of n8n-workflow
59
- */
62
+ // Re-throw structured error for n8n
60
63
if ( error instanceof NodeApiError ) {
61
64
throw error ;
62
65
}
@@ -136,6 +139,7 @@ export async function pollRunStatus(
136
139
method : 'GET' ,
137
140
uri : `/v2/actor-runs/${ runId } ` ,
138
141
} ) ;
142
+
139
143
const status = pollResult ?. data ?. status ;
140
144
lastRunData = pollResult ?. data ;
141
145
if ( [ 'SUCCEEDED' , 'FAILED' , 'TIMED-OUT' , 'ABORTED' ] . includes ( status ) ) {
0 commit comments