Learn how to use the hubspot.fetch
API to fetch data for UI extensions, such as app cards, home pages, and settings pages.
hubspot.fetch()
API. At a high level, to fetch data using this method, you’ll need to:
permittedUrls
in the app’s configuration.hubspot.fetch()
are subject to the following limits:
429
, and can be retried after a delay.timeout
field. Request duration time includes the time required to establish an HTTP connection. HubSpot will automatically retry a request once if there are issues establishing a connection, or if the request fails with a 5XX
status code within the 15 second window.hubspot.fetch()
API along with examples.
hubspot.fetch()
is as follows:
Parameter | Type | Description |
---|---|---|
method | String | The method to use. |
timeout | Number | Time in milliseconds to allow for the request to complete before timing out. Timeout will occur when the back-end request duration exceeds this value or 15 seconds—whichever is smaller. Request duration time includes the time required to establish an HTTP connection.HubSpot will retry a request once if there are issues establishing a connection, or if the request fails with a 5XX status code within the 15 second window. |
body | Object | The request body. |
hubspot.fetch()
does not support request and response headers. The HubSpot signature must be used to perform authentication/authorization in your back-end. For security reasons, you should not store secrets in your React code to communicate with third-party back-ends. Instead, use your own back-end to fetch data from other third-party APIs after validating the HubSpot signature on the request.*-hsmeta.json
configuration file to include the URLs you’ll be requesting. The URLs must be specified in the fetch
array of permittedUrls
.
Note that fetch URLs must be valid HTTPS URLs and cannot be localhost
. If you want to send requests to a locally running back-end, learn how to proxy requests.
hubspot.fetch()
request will still go to your hosted back-end via a HubSpot-managed data fetch service. If you need to update the allowlist while running the dev server, you’ll need to run hs project upload
for the change to take effect.Authorization
header from hubspot.fetch()
. See the example hubspot.fetch() request with Authorization header for more information.
hubspot.fetch()
to pass an Authorization
request header, hubspot.fetch()
does not pass other client-supplied request headers or return response headers set by your back-end server.userId
, portalId
, userEmail
, and appId
. As the request URL is hashed as part of the signature header, this will help you securely retrieve the identity of the user making requests to your back-end.
hubspot.fetch()
requests, check whether you have a local.json
file that’s currently rerouting requests via a proxy. If so, disable this local data fetch feature by renaming the file to local.json.bak
and restarting the development server.hubspot.fetch()
requests made during local development. This proxy is configured through a local.json
file in your project, and will prevent requests from being routed through HubSpot’s data fetch service.
To proxy requests to a locally running back-end:
local.json
file in the same directory as your public-app.json
file. In this file, define a proxy that remaps requests made using hubspot.fetch()
. This mapping will only happen for the locally running extension. You can include multiple proxy key-value pairs in the proxy
object."https://example.com/a": "http://localhost:8080"
hs project upload
.
hs project dev
to start the development server. The CLI should confirm that it has detected your proxy.
hubspot.fetch()
, the CLI will confirm the remapping.
CLIENT_SECRET
environment variable into the local development process.
local.json
file to proxy specific domains, you can inject the CLIENT_SECRET
variable when starting the local development server by prepending the hs project dev
command with the variable:
CLIENT_SECRET
value, as long as you inject the same variable in your locally running back-end that you’re using for hs project dev
. For example, your back-end might include the following Node or Python code:
CLIENT_SECRET
variable:
CLIENT_SECRET
variable from your app into the hs project dev
command permanently. For ease of use, consider baking the variable into your start scripts for local development.
To validate HubSpot request signatures in your custom back-end, check out the request validation guide. You can also use HubSpot’s @hubspot/api-client
npm module to verify requests and sign them yourself. For example:
hubspot.fetch
requests to illustrate promise chaining, async/await, and authorization header usage.
hubspot.fetch()
requests.logger.debug
.