Post

GitHub App - Testing Locally

Testing a GitHub App using Python and Ngrok

GitHub App - Testing Locally

Introduction

Below is the complete working FastAPI example that:

  • Listens on port 3000
  • Handles:
    • POST /webhook — for GitHub webhooks
    • GET /callback — for GitHub OAuth callback

✅ FastAPI Server Creation Steps

  • Create a main.py file for FastAPI Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, PlainTextResponse
import uvicorn

app = FastAPI()

# POST /webhook endpoint
@app.post("/webhook")
async def webhook(request: Request):
    payload = await request.json()
    print(" Received webhook payload:", payload)
    return JSONResponse(content={"status": "received"}, status_code=200)

# GET /callback endpoint
@app.get("/callback")
async def callback(code: str = ""):
    print(f" OAuth callback code: {code}")
    return PlainTextResponse("OAuth callback received.", status_code=200)

if __name__ == "__main__":
    uvicorn.run("main:app", port=3000, reload=True)
  • To Run

  • Install FastAPI + Uvicorn:

1
pip install fastapi uvicorn
  • Run the Server:
1
python main.py
  • It will be available at http://localhost:3000

Sample URLs

  • Webhook URL
    • http://localhost:3000/webhook
    • Use this in your GitHub App to receive event payloads like pushes, PRs, etc.
  • Callback URL
    • http://localhost:3000/callback?code=…
    • GitHub will redirect to this URL after the user authorizes your app.
  • Expose with Ngrok (Optional for GitHub to reach your local server)
1
ngrok http 3000
  • Use the generated public URLs (like https://abc123.ngrok.io/webhook) in your GitHub App setup.
This post is licensed under CC BY 4.0 by the author.