{"slug": "build-a-natural-language-to-sql-api-in-python", "title": "Build a Natural Language to SQL API in Python", "summary": "Telnyx released a Python example that builds a natural language to SQL API using Telnyx AI Inference. The Flask app accepts plain-English questions, generates SQL via an AI model, and includes validation to reject unsafe queries. It also provides a sample SQLite dataset for testing without a production database.", "body_md": "Most data questions start in plain English.\n\n\"Which customers spent the most?\"\n\n\"How many orders are pending?\"\n\n\"What products generated revenue last month?\"\n\nSomeone can turn those questions into SQL, but that usually means waiting on a developer, analyst, or dashboard update.\n\nThis Python example shows how to build a small natural language to SQL API with Telnyx AI Inference.\n\nCode: [https://github.com/team-telnyx/telnyx-code-examples/tree/main/sql-natural-language-python](https://github.com/team-telnyx/telnyx-code-examples/tree/main/sql-natural-language-python)\n\nThe Flask app exposes:\n\n```\nPOST /query\nPOST /query/sample\nPOST /validate\nGET /queries\nGET /queries/<id>\nGET /health\n```\n\n`POST /query`\n\naccepts a natural-language question, SQL dialect, and schema DDL. It returns structured JSON with the generated SQL, explanation, tables used, and metadata.\n\n`POST /query/sample`\n\nuses a bundled SQLite sample dataset, so you can ask a question and see real rows come back without connecting a production database.\n\n`POST /validate`\n\ndry-runs a SQL string against the sample dataset.\n\nNatural language to SQL needs guardrails.\n\nThis example asks the model for read-only SQL and then checks the generated query before execution. The validation layer rejects multiple statements, comments, and write-oriented SQL keywords.\n\nThat keeps the example focused on the useful workflow:\n\nThe model does the language translation. The app still owns the safety checks.\n\nClone the examples repo:\n\n```\ngit clone https://github.com/team-telnyx/telnyx-code-examples.git\ncd telnyx-code-examples/sql-natural-language-python\n```\n\nCreate your `.env`\n\nfile:\n\n```\ncp .env.example .env\n```\n\nAdd your Telnyx API key:\n\n```\nTELNYX_API_KEY=your_telnyx_api_key\nAI_MODEL=moonshotai/Kimi-K2.6\nHOST=127.0.0.1\n```\n\nInstall and start:\n\n```\npip install -r requirements.txt\npython app.py\n```\n\nAsk a question against the sample dataset:\n\n```\ncurl -X POST http://localhost:5000/query/sample \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"question\": \"Show me the top 3 customers by total order revenue\"}' | python3 -m json.tool\n```\n\nValidate a query:\n\n```\ncurl -X POST http://localhost:5000/validate \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"sql\": \"SELECT * FROM orders WHERE total > 100\"}' | python3 -m json.tool\n```\n\nThis is a small API, but it maps to real internal tooling:\n\nThe useful part is the boundary. The app does not blindly trust generated SQL. It asks for structured output, validates the query, and returns JSON that another system can inspect or display.\n\nCode: [https://github.com/team-telnyx/telnyx-code-examples/tree/main/sql-natural-language-python](https://github.com/team-telnyx/telnyx-code-examples/tree/main/sql-natural-language-python)\n\nTelnyx AI skills and toolkits: [https://github.com/team-telnyx/ai](https://github.com/team-telnyx/ai)\n\nTelnyx AI Inference docs: [https://developers.telnyx.com/docs/inference](https://developers.telnyx.com/docs/inference)\n\nChat Completions API: [https://developers.telnyx.com/api/inference/chat-completions](https://developers.telnyx.com/api/inference/chat-completions)\n\nTelnyx Portal: [https://portal.telnyx.com/](https://portal.telnyx.com/)", "url": "https://wpnews.pro/news/build-a-natural-language-to-sql-api-in-python", "canonical_source": "https://dev.to/sonam_50a41a4ced7e6b4f3fa/build-a-natural-language-to-sql-api-in-python-3a7p", "published_at": "2026-07-15 23:21:04+00:00", "updated_at": "2026-07-15 23:34:30.825619+00:00", "lang": "en", "topics": ["natural-language-processing", "developer-tools", "ai-tools"], "entities": ["Telnyx", "Flask", "SQLite", "Kimi-K2.6"], "alternates": {"html": "https://wpnews.pro/news/build-a-natural-language-to-sql-api-in-python", "markdown": "https://wpnews.pro/news/build-a-natural-language-to-sql-api-in-python.md", "text": "https://wpnews.pro/news/build-a-natural-language-to-sql-api-in-python.txt", "jsonld": "https://wpnews.pro/news/build-a-natural-language-to-sql-api-in-python.jsonld"}}