Guide 2.4: Developer Guide

Using SdashAPI with Node.js

Node.js allows you to execute JavaScript on the server. This is the ideal environment for building an API gateway, a GraphQL server, or an Express backend that consumes SdashAPI while keeping your master API key completely hidden from the public frontend.

Using the Native Fetch API

2.4.1

In modern versions of Node.js (v18 and above), the fetch() API is natively supported globally, meaning you no longer need to install libraries like node-fetch or axios just to make simple HTTP requests.

Here is how you would fetch a Post-UTME question in an Express.js route handler, securely pulling your API key from your environment variables:

const express = require('express');
const app = express();

app.get('/get-quiz', async (req, res) => {
  const url = 'https://sdashapi.com/api/v1/q?type=post-utme&subject=biology';
  
  // Securely access the API key from your .env file
  const apiKey = process.env.SDASHAPI_KEY;

  try {
    const apiResponse = await fetch(url, {
      method: 'GET',
      headers: {
        'Accept': 'application/json',
        'AccessToken': apiKey
      }
    });

    const data = await apiResponse.json();

    if (data.status === 200) {
      // Forward the question data to your frontend client
      res.json({
        success: true,
        questionPayload: data.data
      });
    } else {
      res.status(500).json({ success: false, error: "API Error" });
    }

  } catch (error) {
    console.error(error);
    res.status(500).json({ success: false, error: "Network Error" });
  }
});

app.listen(3000, () => console.log('Server running on port 3000'));

Ready to build a UI?

Level 2 is complete. Let's move to Level 3 and integrate with modern UI frameworks.

Next Guide: Using React →
Avatar

How can we help?

We reply immediately

Hello! 👋 How can we help you today?