APIs
To integrate with third party APIs from Cloudflare Workers, use the fetch API to make HTTP requests to the API endpoint. Then use the response data to modify or manipulate your content as needed.
For example, if you want to integrate with a weather API, make a fetch request to the API endpoint and retrieve the current weather data. Then use this data to display the current weather conditions on your website.
To make the fetch()
request, add the following code to your project's src/index.js
file:
If your API requires authentication, use Wrangler secrets to securely store your credentials. To do this, create a secret in your Cloudflare Workers project using the following wrangler secret
command:
Then, retrieve the secret value in your code using the following code snippet:
Then use the secret value to authenticate with the external service. For example, if the external service requires an API key for authentication, include it in your request headers.
For services that require mTLS authentication, use mTLS certificates to present a client certificate.
-
Use the Cache API to cache data from the third party API. This allows you to optimize cacheable requests made to the API. Integrating with third party APIs from Cloudflare Workers adds additional functionality and features to your application.
-
Use Custom Domains when communicating with external APIs, which treat your Worker as your core application.