In the function above, createProjectsCard constructs a card for each project, extracting its details and embedding them into the card template.
Note: The following is a sample code to retrieve projects from the Projects Hub API. So use this code to fetch projects and display them on your site.
app.js
const PROJECTS_HUB_API_KEY = 'USE YOUR OWN API KEY'; // get your API key from https://projectshub.dev/settings
const PROJECTS_HUB_API_URL = 'https://api.projectshub.dev/api/v1/projects'; // the projects hub API URL
// get projects from the API
async function fetchProjects(){
// send a GET request to the projects hub API
const response = await fetch(PROJECTS_HUB_API_URL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${PROJECTS_HUB_API_KEY}`, // set the API key here
},
});
// parse the response to JSON
const data = await response.json();
// throw an error if the request was not successful
if (!data.success) {
throw new Error(data.error.messageDetail || 'Something went wrong!');
}
// return the projects
return data.results.data.projects;
};
The fetchProjects function above retrieves public projects from Projects Hub.
Displaying Projects
Now, let's render these projects on the user interface:
app.js
// render projects to the DOM
const renderProjects = async () => {
// get the projects calling the getProjects function
const projects = await fetchProjects();
// get the projects container element with the class of `.projects`
const projectsEl = document.querySelector('.projects');
// render the projects to the DOM
projects.forEach(project => {
projectsEl.innerHTML += createProjectsCard(project);
});
};
// call the renderProjects function to render the projects to the DOM
renderProjects();
The renderProjects function calls fetchProjects to fetch projects and displays the projects on the UI.
With your account set up, you can now retrieve your public projects from Projects Hub via the API. Let's embark on this journey by initializing a new project. You can either follow along on your local machine or use platforms like .
Congratulations on reaching this point! If you have questions or need further clarification, don't hesitate to join our .