This guide provides step-by-step instructions to configure your .env file and securely use a GitHub Personal Access Token in your FastAPI project.
- Navigate to the
backenddirectory where your project is set up. - Create a new file named
.env(if it doesn’t already exist).
-
Open the
.envfile and add the following line:GITHUB_TOKEN=your_personal_access_token_here -
Replace
your_personal_access_token_herewith your actual GitHub Personal Access Token.
- Go to GitHub Developer Settings.
- Click on Tokens (classic) and then Generate new token and then generate the classic token.
- Select the required scopes for your application:
- For public repositories: Check
repo. - For additional access: Check
read:org,read:user, etc., as needed.
- For public repositories: Check
- Copy the generated token (it will only be displayed once).
Ensure you have these things installed in your project:
pip install python-dotenv requests fastapi uvicorn
Run your FastAPI server:
uvicorn backend.main:app --reloadUse curl or Postman to test the /api/repo-stats endpoint:
curl -X POST http://localhost:8000/api/repo-stats \
-H "Content-Type: application/json" \
-d '{"repo_url": "https://github.com/AOSSIE-Org/Devr.AI/"}'If all the things correctly got setup then you will see the JSON repsponse
Now, your FastAPI application is securely set up to use GitHub API credentials from a .env file!