# Handling 429 rate limits errors from the HubSpot API within a Custom Code block in a Workflow

**URL:** <https://community.hubspot.com/t/handling-429-rate-limits-errors-from-the-hubspot-api-within-a-custom-code-block-in-a-workflow/106791>\
**Category:** Data Hub\
**Created:** [May 9, 2024, 8:21am UTC](https://community.hubspot.com/t/handling-429-rate-limits-errors-from-the-hubspot-api-within-a-custom-code-block-in-a-workflow/106791 "2024-05-09T08:21:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zayne](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/zayne/32/85729_2.png) [@Zayne](https://community.hubspot.com/u/Zayne)\
**Post date:** [May 9, 2024, 8:21am UTC](https://community.hubspot.com/t/handling-429-rate-limits-errors-from-the-hubspot-api-within-a-custom-code-block-in-a-workflow/106791/1 "2024-05-09T08:21:35Z")

</div>

I’m trying to get clarify on the best way to handle rate limiting of the HubSpot APIs from within a Custom Code block in a HubSpot workflow. I have a number of workflows that hit the HubSpot API and I need to gracefully deal with hitting thw rate limits.

There is documentation here that talks about retries for webhook workflows, but this doesn’t seem like it applies:

_[How to Trigger Webhooks in HubSpot Contact-Based Workflows](https://knowledge.hubspot.com/workflows/how-do-i-use-webhooks-with-hubspot-workflows)_

I’ve also read the API docs on this, but this isn’t specific to using code from within a workflow. ([Error handling - HubSpot docs](https://developers.hubspot.com/docs/api/error-handling#:~:text=429%20Too%20many%20requests%20%3A%20returned,over%20its%20API%20rate%20limits.))  
The reason it seems to be impossible to rate limit is that I assume that multiple instances of the workflow can be spawned to deal with any object that satisfies the workflow’s trigger criteria, so how do you limit the rate of calls when you can’t limit how many workflow instances are being run at the same time? Or perhaps my assumption is incorrect about this.

Any advice would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Zayne](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/zayne/32/85729_2.png) [@Zayne](https://community.hubspot.com/u/Zayne)\
**Post date:** [May 9, 2024, 9:11am UTC](https://community.hubspot.com/t/handling-429-rate-limits-errors-from-the-hubspot-api-within-a-custom-code-block-in-a-workflow/106791/2 "2024-05-09T09:11:12Z")

</div>

I managed to find the solution to this. I simply hadn’t read the correct document, which is [here](https://developers.hubspot.com/docs/api/workflows/custom-code-actions?_gl=1*yuh98t*_ga*MTE3MTYwNzc3OC4xNzEyMjk4MzYw*_ga_LXTM6CQ0XK*MTcxNTI0MjEyMS40Mi4xLjE3MTUyNDMwMTUuNjAuMC4w).

My solution is to throw any 429 errors, but to handle all others without throwing, so that I can be notified of non-429 issues through a slack notification further on in the workflow.

```lia-code-sample
 try {
 await axios.request(config)
 has_error = false;
 } catch (e) {
 if (e.response.status === 429) {
 throw e;
 }
 err_msg = e;
 has_error = true;
 }

```

---

<div class="post-metadata">

**Author:** ![aviafriat26](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/aviafriat26/32/82610_2.png) [@aviafriat26](https://community.hubspot.com/u/aviafriat26)\
**Post date:** [May 10, 2024, 10:58pm UTC](https://community.hubspot.com/t/handling-429-rate-limits-errors-from-the-hubspot-api-within-a-custom-code-block-in-a-workflow/106791/3 "2024-05-10T22:58:23Z")

</div>

Hey @Zayne,

To handle 429 rate limit errors effectively within a Custom Code block in a HubSpot workflow, you can implement a few strategies:

1. **Retry Logic** : Implement retry logic directly in your custom code. Use exponential backoff where the time between retries progressively increases after each failed attempt. This can help spread out the requests to avoid hitting the limit.
2. **Limit Request Rate** : Although you can’t directly control the number of workflow instances being executed simultaneously, you can control the rate of API requests within each instance. You might set up a delay or a timer within your custom code to throttle the rate at which requests are made.
3. **Optimize API Calls** : Ensure that each workflow instance uses the minimum number of API calls necessary. Look for ways to batch requests or reduce the need for multiple calls by restructuring how data is handled within the workflow.
4. **Monitor API Usage** : Keep track of your API usage. HubSpot provides tools and analytics to monitor your API call logs. Use this data to understand your usage patterns and adjust accordingly.
5. **Handling Errors** : Properly handle errors in your custom code by checking the response status code and applying conditional logic based on the type of error (e.g., retry on 429, halt on 400, etc.).

These strategies can help manage the load on HubSpot’s API within your workflows and prevent disruptions caused by hitting rate limits.
