Learn to integrate v0 with LearnWorlds using our step-by-step guide. Enhance your online learning experience with easy instructions to 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.
learnworldsClient.ts
. This file will contain the code used to interact with the LearnWorlds API.learnworldsClient.ts
. This class initializes with your API key and includes a sample method getCourses
to call the LearnWorlds "courses" endpoint. (Replace YOURAPIKEY_HERE
with your actual API key.)
class LearnWorldsClient {
private apiKey: string;
private apiBase: string;
constructor(apiKey: string, apiBase: string = 'https://api.learnworlds.com') {
this.apiKey = apiKey;
this.apiBase = apiBase;
}
async getCourses(): Promise {
const response = await fetch(${this.apiBase}/courses, {
method: 'GET',
headers: {
'Authorization': Bearer ${this.apiKey},
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error('Failed to fetch courses. Status: ' + response.status);
}
return response.json();
}
}
export default LearnWorldsClient;
config.ts
in your project directory to store configuration details, such as your LearnWorlds API key and base URL.config.ts
:
export const LEARNWORLDSAPIKEY = 'YOURAPIKEY_HERE'; // Replace with your actual API key
export const LEARNWORLDSAPIBASE = 'https://api.learnworlds.com'; // Change if you use a different base URL
app.ts
or index.ts
).
import LearnWorldsClient from './learnworldsClient';
import { LEARNWORLDSAPIKEY, LEARNWORLDSAPIBASE } from './config';
const learnWorldsClient = new LearnWorldsClient(LEARNWORLDSAPIKEY, LEARNWORLDSAPIBASE);
async function displayCourses() {
try {
const courses = await learnWorldsClient.getCourses();
console.log('List of Courses:', courses);
// Here you can add code to update your UI or further process the courses data
} catch (error) {
console.error('Error fetching courses:', error);
}
}
displayCourses();
getCourses
method. Any data returned is logged to the console for testing purposes.
fetch
API available in most modern environments. If your project environment requires a polyfill for fetch
, add the following code snippet at the very top of your main file (app.ts
or index.ts
):
// If your environment does not support fetch natively, uncomment the following lines and include a fetch polyfill file.
// import 'whatwg-fetch';
<script>
tag in your HTML file (if applicable) or by copying the library files into your project and referencing them appropriately.
console.log
statements.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.