Sooner or later someone asks for "an API" over data that lives in an old system: a SQL Server behind an ERP like Navision, a MySQL database from a 10-year-old PHP app, or an Excel file that the whole office depends on. The tempting shortcut is to open the database port to the integration partner, or to hand them a database user. Please don't.
This post is a checklist for doing it properly, whether you write the API yourself or use a tool.
1. Never open the database to the internet
The database port (1433, 3306, 5432…) should only be reachable from the machine that runs your API. Everything else talks HTTP to the API. That one rule removes a whole class of problems: brute-forced database passwords, leaked connection strings, partners running SELECT * on tables they shouldn't see.
2. Give the API its own read-only database user
Create a dedicated user for the API with the minimum permissions it needs. For most integrations that means SELECT on a handful of tables or views, nothing else: no DROP, no FILE, no access to other databases.
-- SQL Server example CREATE LOGIN api_reader WITH PASSWORD = '<generated, stored outside the code>'; CREATE USER api_reader FOR LOGIN api_reader; GRANT SELECT ON dbo.v_api_customers TO api_reader; GRANT SELECT ON dbo.v_api_orders TO api_reader; If the API needs to write, grant INSERT/UPDATE only on the specific tables, and keep a separate user for it.
3. Expose views, not tables
Legacy schemas are full of columns nobody should see from outside: internal notes, cost prices, personal data. A view lets you choose exactly which columns leave the building and rename them to something readable:
CREATE VIEW dbo.v_api_customers AS SELECT No_ AS id, Name AS name, City AS city, [Blocked] AS blocked FROM dbo.[CRONUS$Customer]; The API reads the view, and the day the ERP schema changes you fix one view instead of every consumer.
4. Whitelist everything that comes from the request
Filters, sort fields and page sizes come from the client, so treat them as hostile:
- Use parameterized queries for every value. No string concatenation, not even for
ORDER BY. - Sort and filter only by columns from an allowlist.
- Cap the page size (for example 100) and always paginate. An unpaginated
GET /orderson a 2-million-row table is a denial of service waiting to happen.
5. Authenticate every call and rate limit it
Each consumer gets its own API key, sent in a header (never in the URL, where it ends up in logs). Store only a hash of the key. Then put limits per key: requests per minute, and lower limits for expensive endpoints like exports. Return 429 with a Retry-After header when a client goes over.
6. Document it from the start
The integration partner will ask "what fields does this return?" on day one. An OpenAPI (Swagger) spec answers that and lets them generate a client in their language. Keep it generated from the same definition as the endpoints so it never drifts.
7. Log access, but not secrets
Log who called what, when, and how long it took. Don't log API keys, passwords or full response bodies with personal data. Those logs are your audit trail when someone asks "who pulled this customer's data?"
Doing it yourself vs. using a tool
If you have a developer and a server, all of the above is a small project: a few views, a thin API layer, a key table and a rate limiter. If you don't, or you need several of these integrations, that's the problem I'm working on with ApiBanana: you connect the source (MySQL, PostgreSQL, SQL Server, MongoDB, REST/SOAP services or Excel/CSV files), pick the fields in a visual builder, and it generates the endpoints with API keys, rate limits and OpenAPI docs. There's a free plan with 10,000 calls a month.
Either way, the principles are the same: the database stays private, the API user can only read what it must, and every request is authenticated, limited and logged.
What's the oldest system you've had to put an API in front of?
