How can i use the API to calculate the number of days a deal is in a certain stage?
Hey @Thulani! ![]()
Great question! To calculate the number of days a deal has been in a certain stage,
you’ll need to track stage transitions using the deal’s activity history.
Here’s the approach:
- Fetch the deal with activities:
GET https://yourdomain.freshsales.io/api/deals/[deal_id]?include=activities
- Filter stage change activities:
Look for activities where activity_type is related to stage changes. The activities
will show timestamps of when the deal moved between stages.
- Calculate the duration:
// Find when deal entered current stage
const stageChangeActivities = activities.filter(a =>
a.activity_type === 'stage_change' &&
a.target_stage === 'Your Stage Name'
);
const enteredStageDate = new Date(stageChangeActivities[0].created_at);
const currentDate = new Date();
const daysInStage = Math.floor((currentDate - enteredStageDate) / (1000 * 60 * 60 * 24));
Alternative approach using Deal History API:
If your Freshsales instance supports it, you can use the deal history endpoint:
GET https://yourdomain.freshsales.io/api/deals/[deal_id]/history
This will give you a timeline of all field changes including stage transitions with
timestamps.
Pro tip:
If you’re building a custom app, you can listen to the onDealUpdate event and track
stage changes in real-time, storing the timestamp in the app’s data storage for quick
access later.
Let me know if you need help with the implementation! ![]()