This is the API for Africa Energy Data based on metrics, countries and units. Built by FastAPI, pymongo.
# AfricaEnergyDataAPI v1
This is the API for Africa Energy Data from 2000 to 2022 based on metrics, countries and units. Built by FastAPI for building endpoints, `pymongo`, Pydantic for data validation, `pytest` for testing middleware and API Key Authentication.
## Features
- Fast and asynchronous REST API using **FastAPI**
- Data endpoints for **Electricity**, **Energy**, and **Economic** indicators
- API key–based middleware authentication
- Pydantic validation for clean, structured data
- Comprehensive test coverage using **pytest**
- Easy deployment on **Render**, and **Docker**
## Setup & Installation
### 1. Clone the Repository
```bash
git clone
github.com
cd AfricaEnergyDataAPI
```
### 2. Create a Virtual Environment
```bash
python -m venv myenv
source myenv/bin/activate # Linux/MacOS
myenv\Scripts\activate # Windows
```
### 3. Install Dependencies
```bash
pip install -r requirements.txt
```
### 4. Set Environment Variables
Create a .env file in the project root:
```ini
APP_ENV=dev
MONGO_URI=mongodb+srv:// : @cluster.mongodb.net/
DEMO_KEY_1=your_demo_api_key
# other env variables
```
### 5. Run the API Locally
```bash
uvicorn app.main:app --reload
```
By default, the server runs on:
127.0.0.1
### 6. Running tests with `pytest`
FastAPI has a module `fastapi.testclient` which allows us to run tests in FastAPI on the fly using `pytest`
To run tests:
```bash
pytest -v
```
### 7. Run Docker Compose
In the same file location as `docker-compose.yml` file, run
```bash
docker compose up --build # For the first time, to install dependencies in requirements.txt
docker compose up -d # After building dependencies, run Docker in detach mode
```
## Endpoints and Example Requests.
### Endpoints
The table below shows the list of the endpoints available in the API for fetching data per sector, checking API health, and testing API Key validation
The API provides access to structured datasets related to …