DEV Community

Cover image for F1 Driver Strategy API – AI-Refined Tactics with Xano
datadr1ven
datadr1ven

Posted on

F1 Driver Strategy API – AI-Refined Tactics with Xano

This is a submission for the Xano AI-Powered Backend Challenge: Production-Ready Public API

What I Built

Hey DEV community! I'm entering the Xano AI-Powered Backend Challenge's "Production-Ready Public API" prompt with my F1 Driver Insights API. This backend service pulls real-time and historical Formula 1 data from the awesome OpenF1 API, enriches it with performance metrics, strategy recommendations, and AI-driven tactical advice—all from a single codebase in Xano. It's designed for fans, devs, or apps needing quick, personalized F1 insights (e.g., "How's Verstappen doing in aggressive mode during rain?").

I focused on making it scalable, secure, and AI-assisted for refinements, while keeping it quota-friendly with a mix of rule-based logic and lightweight AI chaining. The result? A public API that's ready for production, with Swagger docs for easy testing. Let's dive in!

API Documentation

The F1 Driver Insights API provides three core endpoints to deliver tailored race data for any driver in a given meeting (race weekend):

  • GET /f1_driver_meeting_performance?meeting_key=1234&driver_number=1: Fetches raw performance metrics like lap times, positions, and average pace, enriched with a calculated performance score (e.g., win probability based on historical ranks).
  • GET /f1_driver_meeting_strategy?meeting_key=1234&driver_number=1&fanPrefs=aggressive: Adds strategy recommendations, adjusting for user preferences (aggressive, defensive, balanced) with tire wear simulations and pit-stop tips.
  • GET /f1_driver_meeting_ai_strategy?meeting_key=1234&driver_number=1&fanPrefs=aggressive: The x-factor endpoint—uses Xano's AI Function to self-refine the strategy with dynamic, natural-language tactics (e.g., "Push DRS in sector 2, but watch for rain!"), integrating weather data from OpenF1 for realistic morphing.

It's powered by OpenF1 for base data (laps, positions, weather), with Xano handling proxying, caching, security (JWT optional, rate limits), and personalization. Solves: Fans getting bland stats; this API "morphs" them into actionable, preference-based insights. E.g., for Verstappen in a rainy Monaco, aggressive prefs boost overtake scores but warn on aquaplaning.

Demo

Test it live at my public Xano instance: https://x8ki-letl-twmt.n7.xano.io/api:qQiTXlkq/

Example calls (no auth for GETs; use meeting_key from OpenF1 /meetings, driver_number like 1 for Verstappen):

  • Performance: curl "https://x8ki-letl-twmt.n7.xano.io/api:qQiTXlkq/f1_driver_meeting_performance?meeting_key=1267&driver_number=4"
  • Strategy: curl "https://x8ki-letl-twmt.n7.xano.io/api:qQiTXlkq/f1_driver_meeting_strategy?meeting_key=1267&driver_number=4&fanPrefs=aggressive"
  • AI Strategy: curl "https://x8ki-letl-twmt.n7.xano.io/api:qQiTXlkq/f1_driver_meeting_ai_strategy?meeting_key=1267&driver_number=4" (changed to access controlled because my Gemini quota was being exhausted)

Response example for AI strategy (rainy conditions, aggressive prefs):

{
  "driver_number": "1",
  "latest_position": 1,
  "pace_score": 0.95,
  "personalized_score": 0.82,  // Boosted by aggressive but dinged by rain
  "tip": "Hammer the throttle out of turns, but switch to inters for rain!",
  "applied_preference": "aggressive",
  "weather_summary": { "rain": true, "wind_kph": 12, "temp_c": 15 },
  "raw_laps_sample": [ ... ]
}
Enter fullscreen mode Exit fullscreen mode

The AI Prompt I Used

I used XanoScript in VS Code for initial generation, then refined in the dashboard with Logic Assistant for the AI chain.

Original XanoScript prompt (via Copilot for base endpoints):
"Create a REST API for F1 driver insights: GET /races for list (proxy OpenF1 /meetings?year=2025, last 10 sorted), GET /race/{meeting_key}/driver/{driver_number}/insights for laps/positions/weather via OpenF1, with personalization query param fanPrefs. Secure with rate limits, cache 30s."

Logic Assistant prompts for refinements (dashboard chat):

  1. For weather integration: "After fetching laps/positions, add HTTP GET to OpenF1 /weather?meeting_key={{meeting_key}}. Parse body to $weather, merge into $raw. Default dry if failed."
  2. For rule-based personalization: "Add If/Else for fanPrefs (aggressive/defensive/balanced). Compute score from avg position/pace * multiplier (1.18/0.88/1.0). Adjust for weather (rain_mult=0.75 if rain>0). Random tip from map."
  3. For AI self-refine (x-factor in /meeting_driver_ai_strategy): "Add AI Function after rules: Input $raw and fanPrefs. Generate natural-language tip incorporating weather/score (e.g., 'Wet track—go aggressive on inters!'). Merge to $result."

How I Refined the AI-Generated Code

Before: Raw OpenF1 fetches (simple HTTP proxies, no personalization). Slow for repeated calls, no context.

After: Added caching (30s per query for scalability), JWT middleware for optional auth, rate limits (100/min/IP). For x-factor, chained rule-based math (Compute blocks for scores) with AI Function for dynamic tips—e.g., weather dings scores by 25% in rain. Integrated driver filtering on OpenF1 params for specificity.

Code snippet (from Function Stack export):
Before (raw fetch):

HTTP GET OpenF1 /laps?meeting_key={{meeting_key}}&driver_number={{driver_number}} → $laps
Enter fullscreen mode Exit fullscreen mode

After (enriched):

If $weather.rain_intensity > 0, $weather_mult = 0.75 else 1.0
$personalized_score = round(($pace_score * 0.6 + $position_score * 0.4) * $fanPrefs_mult * $weather_mult, 2)
AI Prompt: "Generate tip using $raw, score, weather: e.g., 'Rain? Inters and push!'"
Enter fullscreen mode Exit fullscreen mode

This made it prod-ready: Handles errors (defaults for null data), scales (caching avoids OpenF1 hits), and morphs outputs based on prefs/weather.

My Experience with Xano

As a newbie to Xano, I loved the no-code Function Stack—drag-and-drop HTTP, Compute, and AI blocks saved hours vs. coding a Node server. Logic Assistant nailed prompt-based generation, like auto-building weather chains. Challenge: Free Gemini tier hit 429 on tests; solved with rule fallback for core logic, keeping AI light for tips. Overall, Xano's seamless OpenF1 integration and Swagger auto-docs made this a breeze—highly recommend for AI-assisted backends. Can't wait to expand with webhooks for live race updates!

What do you think, DEV? Upvote if F1 + AI vibes! Source on GitHub: [https://github.com/datadr1ven/f1_insights]. Test and feedback welcome.

Top comments (0)