pip install fastapi uvicorn requests datadog-api-client
### Datadog API Credentials
Obtain your **Datadog API Key** and **Application Key** from the [Datadog API Key Management](https://app.datadoghq.com/organization-settings/api-keys).
## 2. Writing the API Wrapper
**Please note that these are templates for references only, the logic will need to be modified for your particular use case.**
Create a file `api.py`:
```python
from fastapi import FastAPI, HTTPException, Header, Query
import requests
app = FastAPI()
DATADOG_API_KEY = "your_api_key"
DATADOG_APP_KEY = "your_app_key"
DATADOG_LOGS_URL = "https://api.datadoghq.com/api/v2/logs/events/search"
# Step 1: Retrieve container ID
@app.get("/get-container")
def get_container_id(caller_id: str, x_api_key: str = Header(None)):
response = requests.get(DATADOG_LOGS_URL, headers={
"DD-API-KEY": DATADOG_API_KEY,
"DD-APPLICATION-KEY": DATADOG_APP_KEY
}, params={
"query": f"caller_id:{caller_id}",
"sort": "desc"
})
logs = response.json().get("data", [])
if not logs:
raise HTTPException(status_code=404, detail="No logs found for caller_id")
container_id = logs[0].get("attributes", {}).get("container_id", "Unknown")
return {"container_id": container_id}
# Step 2: Fetch logs with filters
@app.get("/get-logs")
def get_logs(container_id: str, tool_keyword: str = Query(None), x_api_key: str = Header(None)):
response = requests.get(DATADOG_LOGS_URL, headers={
"DD-API-KEY": DATADOG_API_KEY,
"DD-APPLICATION-KEY": DATADOG_APP_KEY
}, params={
"query": f"container_id:{container_id} {tool_keyword if tool_keyword else ''}",
"sort": "desc"
})
logs = response.json().get("data", [])
return {"logs": logs}