Square CloudSquare Cloud

How much does it cost to host an API?

A small API usually costs from a few to a few dozen dollars a month, adding up compute, database, bandwidth, requests, logs and a domain. The billing model, fixed or usage-based, decides how the bill grows with traffic.

Updated 8 min read

Learning objectives

After reading this article you will be able to:

  • List the items that make up the monthly cost of an API
  • Estimate the bill line by line with example numbers
  • Compare how a VPS, an always-on container platform and serverless charge
On this page

An API in production looks like it has one price: the machine it runs on. In practice the monthly bill has several lines, and some only show up with traffic. The database can cost more than the API itself, logs grow without anyone noticing, and with usage-based providers every request and every GB sent out lands on the invoice.

This guide separates those lines, shows how to estimate each one and compares the three most common models: a VPS, an always-on container platform and serverless. The prices below are hypothetical round numbers; current rates are on the official pages listed in the sources. For the bill of a whole product, with a front end, email and AI, see how much SaaS infrastructure costs.

What goes into the cost of hosting an API?#

Almost every API pays for six items, even when some are bundled into one price:

  1. Compute: the RAM and CPU your process runs on. On a VPS or a container you pay for reserved capacity, traffic or not. On serverless you pay for the execution time of each call.
  2. Database: on the same machine it has no fee of its own but uses RAM and disk; on a separate service it is another line, often the largest. Price ranges are in how much a cloud database costs.
  3. Bandwidth (egress): the GB your API sends to the internet. Some providers include an allowance, others charge every GB, as the article on egress fees explains.
  4. Requests: serverless functions and API gateways charge per million calls. On a VPS or a container, requests never appear on the invoice; they only affect how big the machine must be.
  5. Logs and observability: many services charge per GB of logs ingested and for retention. Logging every request body produces gigabytes a month.
  6. Domain and TLS: the domain is paid yearly at the registrar, and the TLS certificate usually costs nothing through Let's Encrypt or comes with the platform.

The seventh item never shows up on an invoice: the hours you spend keeping the server running. On a VPS, OS updates, the firewall, the reverse proxy and automatic restarts are your job.

How much capacity does an API need?#

Compute sets the tier you pay for, so measure before you buy. A small REST API in Node.js or Python, with a web framework, an ORM and a database client, usually fits in 256 to 512 MB of RAM. Image processing and models loaded into memory push that number up fast.

Two numbers drive the choice:

  • Peak memory under real load, plus 30% headroom. The method is in how much RAM and CPU your app needs.
  • Requests per second at peak, not on average. An API with 3 million requests a month averages just over one per second, but real traffic bunches up during business hours.

Most APIs spend their time waiting on the database and external services, not computing. That is why a small instance handles more traffic than it seems.

How does each type of solution charge?#

Model How it charges Range for a small API What to watch
VPS Fixed monthly fee for RAM, CPU and disk, with a traffic allowance a few dollars a month OS, security and restarts are on you
Always-on container Monthly fee per instance or per reserved RAM from a few to a few dozen dollars a month every RAM tier changes the price
Serverless (function and gateway) Per invocation, per GB-second and per gateway call near zero at low traffic, growing linearly with use cold starts; bandwidth and logs billed separately

What the table doesn't show:

  • VPS: the low price comes with work. Traffic overage is billed per GB, and promotional prices can rise at renewal, as how much a VPS costs shows.
  • Always-on container: you pay for the instance 24 hours a day. In exchange there are no cold starts, and the bill usually doesn't depend on request count.
  • Serverless: on AWS, Lambda charges each invocation plus duration in GB-seconds (configured memory times execution time), and API Gateway charges per call received and for data sent out. Lambda's monthly allowance keeps the compute of a small API near zero; the gateway, bandwidth and logs are separate lines.

Between the extremes sit on-demand containers like Google Cloud Run, which can bill an instance only while it processes requests, starts or shuts down.

How do you estimate your API's monthly bill?#

You need five numbers: requests per month, average duration, memory, average response size and logs per request. Hypothetical example: 2 million requests a month, 100 ms on average, 512 MB, 20 KB per response and 1 KB of logs per request. That adds up to 40 GB of egress and 2 GB of logs a month.

Hypothetical prices, in dollars, just for the math:

  • 2 GB VPS, with the API and the database on the same machine: $12 a month, with 1 TB of traffic included.
  • Always-on 512 MB container: $10 a month, with 100 GB of traffic included and $0.10 per GB over it.
  • Serverless: $0.30 per million invocations, $0.00002 per GB-second, $1.50 per million gateway calls, $0.10 per GB of egress and $0.40 per GB of logs.
  • Database on a separate service: $15 a month. Domain: $12 a year, or $1 a month.

Serverless compute works out like this: 2 million × 0.1 s × 0.5 GB = 100,000 GB-seconds, which at $0.00002 comes to $2.00. Invocations add $0.60 and the gateway $3.00. Egress costs 40 × 0.10 = $4.00, and logs 2 × 0.40 = $0.80.

Item VPS Always-on container Serverless
Compute $12 (includes the database) $10 $2.60
Database on the same machine $15 $15
Requests (gateway) not charged not charged $3.00
Bandwidth within the allowance within the allowance $4.00
Logs on the VPS disk short retention included $0.80
Domain $1 $1 $1
Total $13 + maintenance hours $26 $26.40

At this volume the three land close together, and the database is the largest line in two of them. What changes with traffic are the usage-based lines. At 20 million requests, egress rises to 400 GB and logs to 20 GB: on serverless both grow in proportion ($40 and $8), while the container only pays for the 300 GB over its allowance ($30), if the instance can handle the peak. The volume at which one model overtakes the other, with duration and memory in the math, is the question of serverless or an always-on server.

Swap in your own numbers and your provider's current prices; the shape of the math doesn't change.

What hidden costs show up later?#

  • Egress. The line that surprises people most: large responses, files served by the API itself and cross-region traffic. Compression (gzip or Brotli) and a CDN for static files cut the volume.
  • Unbounded logs. Logging every request body at debug level makes the volume explode. Set the log level per environment and keep retention short.
  • A second environment. A staging setup with its own API and database repeats part of the bill. Size it for test traffic, not for production.
  • Database connections on serverless. Each function environment opens its own connections, and a spike exhausts the database limit. The fix is usually a connection pooler, one more service on the bill; the typical error is covered in too many connections.
  • Traffic that isn't customers. Bots, scrapers and badly tuned retries create requests you pay for on serverless and that eat capacity on the other models. Rate limiting at the entry point cuts part of it.
  • Currency and taxes. A provider billing in another currency adds exchange-rate swings and foreign transaction fees, and sales tax or VAT may apply on top of the list price.

With usage-based providers, set a budget alert before the first spike: it warns you the day one of these items appears, not when the invoice closes.

How to do this on Square Cloud#

On Square Cloud your API runs in an always-on container, and several lines of the table become the plan's monthly fee, charged in Brazilian reais by Pix or card. Compute is the plan's RAM, a pool shared across your applications, with a 512 MB minimum per web application: Hobby costs R$12.49 a month with 1 GB and 1 vCPU, or R$24.99 with 2 GB and 2 vCPU. Traffic is unlimited, with no charge for bandwidth or requests (fair use policy), so the gateway and bandwidth lines from the example drop out.

The container doesn't sleep or cold start, and every web application gets a *.squareweb.app subdomain with HTTPS. Automatic TLS, DDoS protection, a WAF, real-time logs and metrics, and automatic restart when the process exits with an error come with every plan, which cuts the maintenance hours of a VPS.

Database hosting and custom domains start on Standard, from R$44.99 a month with 4 GB: the database uses the plan's RAM (1024 MB minimum for PostgreSQL, MySQL and MongoDB), and the domain stays at your registrar. There is no autoscaling: to grow, you raise the application's RAM or split the domain across several applications with load balancing. See runtimes and examples on API hosting.

API hosting

APIs in always-on containers, with no cold starts

API hosting

FAQs

How much does it cost to host a small API?

The compute for a small API usually fits in 256 to 512 MB of RAM, the entry tier of most VPS and platforms, in the range of a few dollars a month. Add the database, the domain and, with usage-based providers, requests, bandwidth and logs.

What is the biggest cost of hosting an API?

For a small API, it is usually the database, when it runs on a separate service. With high traffic on a usage-based provider, egress and logs start to weigh as much as the database.

Does API hosting charge per request?

It depends on the model. Serverless functions and API gateways charge per million calls. A VPS or a container platform charges for reserved capacity, and request volume only affects the size of the machine.

Why is my API bill higher than expected?

The usual suspects are data egress, log volume, an oversized database and traffic from bots or retries. Set a budget alert with your provider before the first spike.

Next articleCORS error (blocked by CORS policy): cause and fixA CORS error means the API did not allow your page's origin. Learn the causes, how to fix it in Express and FastAPI, and how to test the preflight with curl.