datasquirel-docs/pages/api-reference/sql/options/index.mdx

82 lines
2.0 KiB
Plaintext

---
title: API SQL OPTIONS | Datasquirel docs
description: Execute a raw SQL query via the Datasquirel API
page_title: SQL OPTIONS
page_description: Execute any valid MariaDB SQL statement and receive results as JSON.
---
## Overview
The SQL OPTIONS endpoint accepts any valid MariaDB SQL string and executes it against your database. Results are returned as a JSON array.
## npm Package
```javascript
import datasquirel from "@moduletrace/datasquirel";
const result = await datasquirel.api.sql({
key: process.env.DATASQUIREL_API_KEY,
params: {
query: "SELECT posts.id, posts.title, users.name AS author FROM posts JOIN users ON posts.user_id = users.id WHERE posts.is_published = 1 ORDER BY posts.created_at DESC LIMIT 20",
},
});
console.log(result.payload);
```
### Aggregation Query
```javascript
const result = await datasquirel.api.sql({
key: process.env.DATASQUIREL_API_KEY,
params: {
query: "SELECT category, COUNT(*) AS total FROM posts GROUP BY category ORDER BY total DESC",
},
});
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `key` | `string` | Yes | Full Access API key |
| `params.query` | `string` | Yes | The raw MariaDB SQL string to execute |
## REST API
```
POST /api/v1/sql
```
**Headers:**
```
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
```
**Body:**
```json
{
"query": "SELECT * FROM users WHERE is_active = 1 LIMIT 10"
}
```
## Response
```json
{
"success": true,
"payload": [
{ "id": 1, "name": "Alice", "email": "alice@example.com" },
{ "id": 2, "name": "Bob", "email": "bob@example.com" }
]
}
```
## Notes
- A **Full Access** API key is required.
- All valid MariaDB statements are supported: SELECT, INSERT, UPDATE, DELETE, ALTER, CREATE, DROP, and more.
- Be careful with DDL statements (ALTER, DROP) — they modify the table structure and cannot be undone.
- Parameterized queries are not supported through this endpoint — sanitize user input before embedding it in a SQL string.