Discover how to integrate Bolt.new AI with the Khan Academy API. Follow our step-by-step guide to create interactive educational tools and streamline your workflow.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
khanAcademyAPI.ts
. This file will hold all functions related to interacting with the Khan Academy API.khanAcademyAPI.ts
. This code defines an interface for content items, a constant for the API base URL, and a function fetchContent
that calls Khan Academy’s API using the built-in fetch
function. Adjust the mapping as needed depending on the actual API response structure.
export interface ContentItem {
title: string;
description: string;
url: string;
}
const APIBASEURL = 'https://www.khanacademy.org/api/v1';
export async function fetchContent(topic: string): Promise<ContentItem[]> {
const response = await fetch(${API_BASE_URL}/topic/${topic});
if (!response.ok) {
throw new Error(Error fetching data: ${response.statusText});
}
const data = await response.json();
// Adjust this mapping based on the actual structure returned by the Khan Academy API.
// For example, assume data.content contains an array of items.
return data.content.map((item: any) => ({
title: item.title,
description: item.description,
url: item.url,
}));
}
index.ts
) where you want to use the Khan Academy data.fetchContent
function from khanAcademyAPI.ts
and call it within an asynchronous function. The following snippet demonstrates how to fetch content for a given topic (for example, "math") and log the data to the console.
import { fetchContent } from './khanAcademyAPI';
async function displayContent() {
try {
const contentItems = await fetchContent('math'); // Replace 'math' with your desired topic
console.log('Khan Academy content:', contentItems);
} catch (error) {
console.error('Error fetching content:', error);
}
}
displayContent();
config.ts
in your project.config.ts
to centrally manage your configuration values. Replace 'yourapikey_here'
with your actual API key. If the Khan Academy API does not require one, you can skip this step.
export const KHANAPIKEY = 'yourapikey_here'; // Replace with your actual API key if needed
khanAcademyAPI.ts
to include the API key in your requests. For example:
import { KHANAPIKEY } from './config';
export async function fetchContent(topic: string): Promise<ContentItem[]> {
const response = await fetch(${API_BASE_URL}/topic/${topic}?api_key=${KHAN_API_KEY});
// ... rest of the code remains the same ...
}
npm install
to add dependencies manually.fetch
function for making HTTP requests, so there is no need for an external library like Axios.
khanAcademyAPI.ts
and optionally config.ts
) are saved in your project.index.ts
) in the Bolt.new AI interface to see if the Khan Academy API call is working as expected.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.