Master the integration of Bolt.new AI with the Fitbit API. Follow our step-by-step guide to merge advanced AI with fitness tracking for enhanced health insights.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
https://dev.fitbit.com
and create a new application to obtain your clientid
and clientsecret
.https://your-app.com/fitbit/callback
).
package.json
file in the root of your project if it doesn’t exist yet.axios
by pasting the following JSON code into the file. This ensures that the required modules are installed when the project loads.
{
"name": "bolt-fitbit-integration",
"version": "1.0.0",
"dependencies": {
"axios": "^0.27.2"
}
}
fitbit.ts
in your project’s source directory.fitbit.ts
. Replace environment variables as needed.
import axios from 'axios';
const CLIENTID = process.env.FITBITCLIENT_ID as string;
const CLIENTSECRET = process.env.FITBITCLIENT_SECRET as string;
const REDIRECTURI = process.env.FITBITREDIRECT_URI as string;
export function getAuthUrl(): string {
const scopes = 'activity profile';
const url = https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=${CLIENT_ID}&redirect_uri=${encodeURIComponent(REDIRECT_URI)}&scope=${encodeURIComponent(scopes)};
return url;
}
export async function exchangeCodeForToken(code: string): Promise {
const tokenUrl = 'https://api.fitbit.com/oauth2/token';
const authHeader = Buffer.from(${CLIENT_ID}:${CLIENT_SECRET}).toString('base64');
const params = new URLSearchParams();
params.append('clientid', CLIENTID);
params.append('granttype', 'authorizationcode');
params.append('redirecturi', REDIRECTURI);
params.append('code', code);
try {
const response = await axios.post(tokenUrl, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': Basic ${authHeader}
}
});
return response.data;
} catch (error) {
console.error('Error exchanging code for token:', error);
throw error;
}
}
routes.ts
in your project’s source directory to handle HTTP routes for Fitbit OAuth.fitbit.ts
:
import express from 'express';
import { getAuthUrl, exchangeCodeForToken } from './fitbit';
const router = express.Router();
// Route to redirect users to Fitbit for authorization
router.get('/fitbit/auth', (req, res) => {
const authUrl = getAuthUrl();
res.redirect(authUrl);
});
// Callback route to handle Fitbit response
router.get('/fitbit/callback', async (req, res) => {
const code = req.query.code as string;
if (!code) {
return res.status(400).send('Authorization code not provided.');
}
try {
const tokenData = await exchangeCodeForToken(code);
// Process the tokenData (e.g. save tokens for future API calls)
res.json(tokenData);
} catch (error) {
res.status(500).send('Error exchanging code for token.');
}
});
export default router;
index.ts
or app.ts
) in your Bolt.new AI project.routes.ts
.
import express from 'express';
import fitbitRoutes from './routes';
const app = express();
// Middleware for parsing JSON if needed
app.use(express.json());
// Use the Fitbit API routes
app.use(fitbitRoutes);
// Default route to test the server
app.get('/', (req, res) => {
res.send('Bolt.new AI Fitbit integration is running.');
});
// Start the server on the port provided by Bolt.new or a default port
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(Server is listening on port ${PORT});
});
FITBITCLIENTID
– your Fitbit client IDFITBITCLIENTSECRET
– your Fitbit client secretFITBITREDIRECTURI
– the callback URL you registered with Fitbit
/
) to ensure your server is up./fitbit/auth
to be redirected to Fitbit’s authorization page. After authorizing, Fitbit will redirect you to your callback URL where the access token details are returned as JSON.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.