Docs
Connect to a Database

Connect to a Database

MaskAPI makes it easy to connect your APIs to real databases like PostgreSQL and MySQL whether across your entire project or just a single endpoint. With database mode, you can build powerful, production-ready APIs that read from or write to your own data sources using SQL and JavaScript.


📘 What is Database Mode?

In our studio, Database Mode lets you write API logic that uses a live connection to your database. You can:

  • Query or mutate your data using SQL

  • Handle results using JavaScript

  • Return fully customized responses


🧩 How to Connect Your Project to a Database

If you'd like to connect all endpoints in your project to a single database, follow these steps:

  1. Select your project

  2. Go to the "Settings" tab

  3. Under "Project Global Configurations" section

  4. Add your database connection string in "Database Connection" field

  5. Click "Save Changes" on the top of the page

All new endpoints in the project will now use this database connection by default.

MaskAPI supports only pooled database connections. Use providers like Supabase, Neon, Planetscale, or a managed DB with pooling enabled.


⚙️ How to Connect a Single Endpoint to a Database

If you prefer to connect just a specific endpoint to a specific database, follow these steps:

  1. Select your endpoint

  2. Go to the "Config" tab

  3. Under "Configuration" table

  4. Click "Set Custom Config"

  5. Add your database connection string in "Database Connection" field

This will override the default project-wide connection (if any) for this endpoint only.


🐘 How to Write an Endpoint with PostgreSQL

If your endpoint uses PostgreSQL, MaskAPI uses the pg library under the hood. You can write your endpoint like this:

return (async function (connection, maskRoutes, maskQueries, maskRequestHeaders, maskRequestBody){
  const { rows } = await connection.query(`SELECT * FROM users;`);
  
  return {
    responseBody: {
      result: rows,
    },
    responseHeaders: {
      'content-type': 'application/json'
    },
    statusCode: 200
  }
})(connection, maskRoutes, maskQueries, maskRequestHeaders, maskRequestBody);

🐘 How to Write an Endpoint with MySQL

If your endpoint uses MySQL, MaskAPI uses the mysql2 library under the hood. You can write your endpoint like this:

return (async function (connection, maskRoutes, maskQueries, maskRequestHeaders, maskRequestBody){
  const [rows] = await connection.query(`SELECT * FROM users;`);
 
  const data = rows.map(row => ({ ...row }));
 
  return {
    responseBody: {
      result: data,
    },
    responseHeaders: {
      'content-type': 'application/json'
    },
    statusCode: 200
  }
})(connection, maskRoutes, maskQueries, maskRequestHeaders, maskRequestBody);

🧪 How to Test Your Connection

In order to test your API:

  • Use the Playground inside the Studio to send live test requests
  • Use console.log() inside your endpoint to inspect values
  • View logs and debug output directly in MaskAPI Studio

Use proper query parameterization to prevent SQL injection.