Skip to content
Notifications
Clear all

How-to: Use the API to build a simple rep leaderboard.

2 Posts
2 Users
0 Reactions
27 Views
(@jamesw)
Trusted Member
Joined: 3 months ago
Posts: 48
Topic starter   [#12302]

I see a lot of teams using Consensus manually, which is fine for a one-off check. But if you're trying to build a culture of continuous improvement, you need visibility. An automated leaderboard is the simplest way to do that.

The Consensus API is straightforward. You'll need your API key from the team settings. For this example, we're pulling the last 30 days of rep data for all team members and ranking them. I'm using Python because it's readable, but the principle is the same in any language.

First, get your data. The key endpoint is `/v1/team/reputation`. You can filter by date range. This returns a JSON object with user details and their total rep for the period.

```python
import requests
from datetime import datetime, timedelta

API_KEY = "your_api_key_here"
BASE_URL = "https://api.consensus.com"
headers = {"Authorization": f"Bearer {API_KEY}"}

end_date = datetime.now()
start_date = end_date - timedelta(days=30)

params = {
"start_date": start_date.isoformat(),
"end_date": end_date.isoformat()
}

response = requests.get(f"{BASE_URL}/v1/team/reputation", headers=headers, params=params)
data = response.json()
```

Once you have the data, sort it and format it for display. You can output this to a simple HTML page, a Slack message, or a dashboard widget. The point is to get it out of the app and into a place people see daily.

```python
users = data.get('users', [])
sorted_users = sorted(users, key=lambda x: x['reputation_points'], reverse=True)

leaderboard = []
for rank, user in enumerate(sorted_users, start=1):
leaderboard.append(f"{rank}. {user['name']}: {user['reputation_points']} pts")

print("\n".join(leaderboard))
```

Common pitfalls:
* Not caching the results. Don't hit the API on every page load.
* Forgetting to handle deactivated users. Filter them out.
* The date range is in UTC. Account for your timezone if needed.

This is a basic version. From here, you can add trends (up/down arrows), link to specific feedback, or segment by department. The goal is to move from opaque to transparent.

—JW


—JW


   
Quote
(@jasonc)
Estimable Member
Joined: 3 months ago
Posts: 60
 

Good starting point, but I'd caution against running that script on a schedule without some caching. Hitting the `/v1/team/reputation` endpoint on every leaderboard refresh can get rate-limited if you have a lot of users. It's more efficient to subscribe to the reputation webhook and maintain a local tally. That way, your leaderboard data is always current and you only make one API call to seed the initial state.

Also, the example sorts by total rep, but for a true "improvement" leaderboard, you might want delta from the previous period. That requires storing historical snapshots.

A slightly more resilient approach would be:
1. Initial fetch to populate your database.
2. Webhook listener to increment/decrement scores.
3. A separate, cached read endpoint for the leaderboard UI.

Your script would be the perfect one-off to run for that initial population step.


API whisperer


   
ReplyQuote