You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b9a7b2f
Browse filesBrowse the repository at this point in the historyBrowse files
docs(academy): use fetch and Authorization header in API & client solution (#3288)
Updates the API mode of the "Using the Apify API & JavaScript client"
solution:
- Uses Node.js's built-in `fetch` instead of `axios`, which the empty
JavaScript template doesn't install. Closes#3279.
- Sends `APIFY_TOKEN` in an `Authorization: Bearer` header instead of a
`token` query parameter, since tokens in URLs tend to end up in logs.
Closes#3280.
Also fixes a few wording nits on the page along the way.
---------
Co-authored-by: Claude <noreply@anthropic.com>
## Calling a task via API {#calling-a-task-via-api}
100
99
101
-
First, we'll create a function (right under the `withClient()`) function named `withAPI` and instantiate a new variable which represents the API endpoint to run our task:
100
+
First, right under `withClient()`, we'll create a function named `withAPI()` and define the API endpoint for running our task:
To add the query parameters to the URL, we could create a long string literal, plugging in all of our input values. However, there is a much better way: [`URLSearchParams`](https://nodejs.org/api/url.html#new-urlsearchparams). By using `URLSearchParams`, we can add the query parameters in an object:
Finally, let's make a `POST` request to our endpoint. You can use any library you want, but in this example, we'll use [`axios`](https://www.npmjs.com/package/axios). Don't forget to run `npm install axios` if you're going to use this package too!
124
+
Finally, let's make a `POST` request to our endpoint. We'll use [`fetch`](https://nodejs.org/api/globals.html#fetch), which is provided by Node.js out of the box.
125
+
126
+
The request needs to be authenticated with an API token, which is available in the `APIFY_TOKEN` environment variable. We'll send it in the `Authorization` header:
0 commit comments