Getting Started with the Streaks API
The Streaks API allows you to integrate streak tracking into your applications, perfect for fitness apps, habit trackers, educational tools, and more. Follow these steps to get up and running.
1. Create an Account and Generate an API Key
To use the Streaks API, you first need to create an account and generate an API key from the project page. This key will be used to authenticate your requests to the API. See the Authentication Documentation Page for more information.
2. Start Sending Your Events
After obtaining your API key, you can start sending events related to user activities. See the Create Events page for more information on the event object. Here's a basic example of how to send an event using JavaScript:
import axios from 'axios';
const createEvent = async (projectId, eventData) => {
try {
const response = await axios.post(`https://api.streaksapi.com/events/project/${projectId}`, eventData, {
headers: {
'x-api-key': 'YOUR_API_KEY'
}
});
console.log('Event Created:', response.data);
} catch (error) {
console.error('Error creating event:', error);
}
};
createEvent('123', {
userId: '456',
streakId: '789',
timestampISO: '2023-12-06T00:00:00.000Z'
});
3. Calculate a Streak
After you've sent some events, you can calculate a streak for a given user or streakId. See the Get Streak page for more information on the various options. Here's an example of how to calculate a streak using JavaScript:
import axios from 'axios';
const getCurrentStreak = async (projectId, streakId, config) => {
try {
const response = await axios.get(`https://api.streaksapi.com/events/${projectId}/currentStreak/${streakId}`, {
params: config,
headers: {
'x-api-key': 'YOUR_API_KEY'
}
});
console.log('Current Streak:', response);
} catch (error) {
console.error('Error fetching current streak:', error);
}
};
getCurrentStreak('123', '456', {
frequencyType: 'daily',
customFrequencyMS: 0,
weekStartDay: 'Monday',
countSamePeriod: false
});