> ## Documentation Index
> Fetch the complete documentation index at: https://doc.eazemyapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom queries

EazeMyAPI allows you to write **custom database queries** and expose them as API endpoints.

This feature is useful when you need more control than the automatically generated CRUD APIs.

With custom queries you can:

* Join multiple tables
* Filter complex data
* Create custom reports
* Build advanced API endpoints

***

## When to Use Custom Queries

Use custom queries when:

* You need **JOIN operations**
* You want **custom filters**
* You need **aggregated data**
* You want **custom endpoints for frontend apps**

### Example Use Cases

* Fetch posts by category
* Get user statistics
* Build dashboard analytics
* Filter records by multiple conditions

***

## Example Query

Example query that retrieves posts by category:

```sql theme={null}
SELECT 
  categories.id AS category_id,
  categories.name AS category_name,
  posts.id AS post_id,
  posts.description
FROM categories
JOIN posts ON posts.category_id = categories.id
WHERE categories.name = '{category_name}';
```

***

## Generated API Endpoint

Once a custom query is saved, EazeMyAPI automatically generates an API endpoint.

```
https://api.eazemyapi.com/{project}/{version}/{query-name}
```

### Example

```
https://api.eazemyapi.com/demo-project/v1/posts-by-category
```

***

## Example API Request

```bash theme={null}
curl https://api.eazemyapi.com/demo-project/v1/posts-by-category \
  -H "X-API-SIGNATURE: your-secret-key"
```

***

## Example API Response

```json theme={null}
{
  "success": true,
  "message": "Data found.",
  "data": [
    {
      "category_id": "c40a4731-a541-4d2b-af77-326af7142aab",
      "category_name": "Foods",
      "post_id": "2529c967-33a3-4f7b-aabd-84e90f861e14",
      "description": "Best restaurant in the world"
    },
    {
      "category_id": "c40a4731-a541-4d2b-af77-326af7142aab",
      "category_name": "Foods",
      "post_id": "5f8b35c8-372c-4ab2-9c01-569085f05a72",
      "description": "Best restaurant in Ahmedabad"
    }
  ]
}
```

***

## Creating a Custom Query

To create a custom query in the dashboard:

1. Open your **Project**
2. Navigate to **Queries** or **Custom Queries**
3. Click **Create Query**
4. Write your SQL query
5. Save the query

Once saved, an API endpoint will automatically be generated.

***

## Query Parameters

Custom queries can accept parameters.

### Example Query

```sql theme={null}
SELECT *
FROM posts
WHERE category_id = '{category_id}';
```

### Example API Request

```
https://api.eazemyapi.com/demo-project/v1/posts-by-category?category_id=c40a4731-a541-4d2b-af77-326af7142aab
```

***

## Best Practices

When writing custom queries:

* Keep queries optimized
* Use indexes when possible
* Avoid returning unnecessary columns
* Validate input parameters
* Use parameter binding (avoid raw string injection)

Well-written queries improve API performance and security.

***

## Related Documentation

Learn how database tables work:

[Database Schema](https://doc.eazemyapi.com/docs/database-schema.md)

Understand API endpoints:

[API Endpoints](https://doc.eazemyapi.com/docs/api-endpoints.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.