Skip to content

HTTP API Guide ​

MygramDB provides a RESTful JSON API for easy integration with web applications and HTTP clients.

How this differs from the TCP API

The HTTP API is the JSON entry point for search, count, facet, and document lookup. Operational commands such as SYNC, DUMP, and SET are run over TCP/CLI.

Configuration ​

Enable the HTTP server in your config.yaml:

yaml
api:
  tcp:
    bind: "127.0.0.1"
    port: 11016
  admin_token: "replace-with-a-secret"
  http:
    enable: true          # Enable HTTP server
    bind: "127.0.0.1"     # Bind address (default: localhost only)
    port: 8080            # HTTP port (default: 8080)
    max_connections: 10000 # Includes sockets waiting for an HTTP worker
    max_body_bytes: 16777216 # Request-body cap (16 MiB by default)
    trusted_proxies: []   # Numeric reverse-proxy IPs allowed to supply X-Forwarded-For
    enable_cors: false    # Optional: enable only when exposing to browsers
    cors_allow_origin: "" # Required when enable_cors is true

WARNING

TCP/HTTP servers bind to loopback by default. If you must expose them publicly, explicitly set api.tcp.bind/api.http.bind, configure network.allow_cidrs to the exact IP ranges that should connect, and keep TLS/API authentication in front of MygramDB (e.g., reverse proxy). CORS is disabled by default and should only be enabled with a trusted origin.

SettingBehavior
api.http.max_connectionsCaps admitted HTTP sockets, including sockets waiting for an HTTP worker.
api.http.max_body_bytesRejects POST bodies above this size with 413 Payload Too Large before routing. Default: 16 MiB.
api.http.trusted_proxiesAllowlist of numeric proxy IP addresses that may supply X-Forwarded-For.

Entries in trusted_proxies are normalized before matching, so an IPv4 entry matches both a.b.c.d and its IPv4-mapped IPv6 form ::ffff:a.b.c.d on a dual-stack listener. MygramDB honors X-Forwarded-For only when the direct TCP peer is on the list, and ignores forwarding headers from other clients. The resulting client identity is normalized the same way as for TCP, so ACL checks, rate limiting, and logs use the same identity on both surfaces. The rate-limit bucket is shared across protocols.

Starting a second instance with the same HTTP bind address and port fails with Bind failed (6000) (kNetworkBindFailed).

What is CORS?

CORS is the browser mechanism that permits a page to call an API on another domain. Server-to-server traffic does not need it. Enable it only when a browser calls MygramDB directly, and restrict it to a trusted origin.

API Endpoints ​

RoutePurposeAdmin token (api.admin_token)
POST /tables/{identity}/searchFull-text searchNot used
POST /tables/{identity}/countCount matchesNot used
POST /tables/{identity}/facetFacet buckets for a columnNot used
GET /tables/{identity}/{primary_key}Fetch one documentNot used
POST /optimizeCompact posting listsRequired
GET /infoStatistics as JSONNot used
GET /metricsPrometheus metricsOptional; needed for replication metrics
GET /health, GET /health/liveLoad balancer check, liveness probeNot used
GET /health/readyReadiness probeOptional; needed for replication error counters
GET /health/detailDetailed monitoring snapshotOptional; needed for replication details
GET /configRedacted configuration summaryRequired
GET /replication/statusMySQL replication statusRequired

"Required" and "Optional" apply only when api.admin_token is non-empty; without a token these routes are open. Every route is subject to the network.allow_cidrs allowlist. For HTTP, that check runs after the request headers are parsed and before the body is read. A denied peer receives 403 without its body being read, but occupies a connection slot until the check. The allowlist is not an HTTP socket-accept filter.

Handler-generated responses return JSON with Content-Type: application/json, except /metrics, which returns Prometheus text. CORS preflight OPTIONS requests return 204 No Content without a body.

Table routes use a single identity segment, /tables/{identity}. {identity} is the database-qualified name `database.table`; for example, table articles in database app_db is addressed as /tables/app_db.articles. When only one database is configured, a bare table name also works and resolves to that database (/tables/articles). A qualifier is required only when the configuration spans two or more databases. A bare name is then rejected as ambiguous with 404 Not Found and error_code: 4007.

Thinking about identity

identity is the name that uniquely designates the table to search. With one database it is articles; with several it is app_db.articles.

The legacy /{table}/search, /{table}/count, and /{table}/{primary_key} routes were removed in v1.7.0. Use /tables/{identity}/... for all table operations.

Paths are percent-decoded before routing. The document GET route is dispatched manually, so decoded paths longer than 256 bytes still reach the handler (subject to the URI limit); a %2F in a primary key is available as /.

POST request settings belong in the JSON body; query-string parameters are ignored. JSON bodies with invalid syntax or more than 32 nested arrays/objects return 400 Bad Request with error_code: 3000. HEAD requests use the corresponding GET handler.

POST /tables/{identity}/search ​

Full-text search with filters and pagination.

Request:

http
POST /tables/app_db.threads/search HTTP/1.1
Content-Type: application/json

{
  "q": "breaking news AND tech",
  "mode": "boolean",
  "filters": {
    "status": 1,
    "category": "tech"
  },
  "limit": 50,
  "offset": 0
}

Request Body Parameters:

FieldTypeRequiredDescription
qstringYesSearch text; treated literally unless mode is "boolean"
modestringNo"literal" (default) or "boolean"
filtersobjectNoFilter conditions as scalar values or { "op": "...", "value": ... } objects
limitintegerNoMaximum results to return (default: 100, max: 1000)
offsetintegerNoNumber of results to skip (default: 0)
sortobjectNoSort configuration (e.g., {"column": "_score", "order": "DESC"})
highlightobjectNoHighlight configuration (see below). Each result then carries a highlight snippet string
fuzzyintegerNoFuzzy search edit distance (1 or 2)

limit must be between 1 and 1000. A value above 1000 or more than 64 filter conditions returns 400 Bad Request with error_code: 3000. A non-integer limit or a value below 1 returns error_code: 3008.

Omit sort.column to sort by the primary key. To sort by a named column, provide that column and order: "ASC" or "DESC"; for example, { "sort": { "column": "created_at", "order": "DESC" } }.

HTTP treats q as literal text by default. For example, "AND" and "LIMIT" are ordinary search terms, not operators or control clauses. To send a boolean expression, set "mode": "boolean" explicitly:

json
{
  "q": "tech AND (AI OR machine learning) NOT old",
  "mode": "boolean",
  "limit": 50
}

Boolean Query Syntax (mode: "boolean"):

SyntaxExample q valueNote
Simple search"keyword"
Quoted phrase"\"breaking news\""Searches for the exact phrase
AND"tech AND AI AND machine learning"
OR"(mysql OR postgresql) AND performance"
NOT"news NOT sports"
AND NOT"news AND NOT sports"Equivalent exclusion
Combined"tech AND AI NOT old"

Use JSON fields for limit, offset, sort, filters, highlight, and fuzzy. q never accepts those control clauses; in literal mode, text such as LIMIT or SORT remains part of the search text.

Each filter can use a scalar value (which defaults to equality) or an operator object. The object form requires a value; op is optional and defaults to EQ, but when present it must be a string, for example:

json
{
  "filters": {
    "price": { "op": "GTE", "value": 100 },
    "status": { "op": "EQ", "value": "active" }
  }
}

Supported operators are EQ, NE, GT, GTE, LT, and LTE (their symbolic forms are also accepted). A non-string op is rejected with 400 Bad Request and error_code: 3006 (kQueryInvalidFilter). Unsigned JSON integers retain their 64-bit value, so an unsigned BIGINT greater than INT64_MAX is compared without wrapping.

Boolean mode uses the same Unicode whitespace tokenizer as the TCP API. For example, an ideographic space (U+3000) separates terms just like an ASCII space.

Response (200 OK):

json
{
  "count": 2,
  "limit": 50,
  "offset": 0,
  "results": [
    {
      "primary_key": "article_101",
      "filters": {
        "status": 1,
        "category": "tech"
      }
    },
    {
      "primary_key": "article_205",
      "filters": {
        "status": 1,
        "category": "tech"
      }
    }
  ]
}

Error Response (400 Bad Request):

json
{
  "error": "Missing required field: q",
  "error_code": 3000
}

Error Response (500 Internal Server Error):

json
{
  "error": "Internal server error",
  "error_code": 5
}

Highlight Configuration:

FieldTypeDefaultDescription
open_tagstring<em>Opening tag for highlighted terms
close_tagstring</em>Closing tag for highlighted terms
snippet_lengthinteger100Max code points per snippet (1-10,000)
max_fragmentsinteger3Max snippet fragments (1-100)

Search with Highlighting Example:

http
POST /tables/app_db.articles/search HTTP/1.1
Content-Type: application/json

{
  "q": "machine learning",
  "highlight": {
    "open_tag": "<strong>",
    "close_tag": "</strong>",
    "snippet_length": 150,
    "max_fragments": 5
  },
  "sort": {"column": "_score", "order": "DESC"},
  "limit": 10
}

POST /tables/{identity}/count ​

Count documents matching a full-text query and optional filters. COUNT returns only the total count; pagination, highlighting, fuzzy search, and _score sorting are search-only features and are rejected on this endpoint.

Request:

http
POST /tables/app_db.threads/count HTTP/1.1
Content-Type: application/json

{
  "q": "breaking news AND tech",
  "mode": "boolean",
  "filters": {
    "status": 1
  }
}

Response (200 OK):

json
{
  "count": 42
}

POST /tables/{identity}/facet ​

Return facet buckets for a column, optionally scoped by a query and filters.

Request Body Parameters:

FieldTypeRequiredDescription
columnstringYesFilter column to aggregate
qstringNoSearch query used to scope the facet
modestringNo"literal" (default) or "boolean" for q
filtersobjectNoAdditional filter conditions
limitintegerNoMaximum number of facet buckets (1-1000)
offsetintegerNoNumber of facet buckets to skip (default: 0)

sort, highlight, and fuzzy are not supported by FACET and are rejected. A syntactically invalid facet column name returns 400 Bad Request with error_code: 3000. A well-formed name that does not identify a configured filter column returns 400 with error_code: 4000.

Request:

http
POST /tables/app_db.articles/facet HTTP/1.1
Content-Type: application/json

{
  "column": "category",
  "q": "database OR mysql",
  "mode": "boolean",
  "filters": {
    "status": 1
  },
  "limit": 2
}

Response (200 OK):

json
{
  "column": "category",
  "count": 2,
  "total_count": 17,
  "facets": [
    {"value": "tech", "count": 15},
    {"value": "ops", "count": 7}
  ]
}

count is the number of facet entries returned on this page. total_count is the number of distinct facet values before limit and offset; use it to calculate pagination.

HTTP Surface ​

The HTTP API exposes search, count, document lookup, health, metrics, replication status, redacted configuration inspection, and POST /optimize. SET, SHOW VARIABLES, SYNC, and DUMP remain TCP/CLI commands and have no HTTP route.

POST /optimize ​

Compacts posting lists for one table or every configured table. Send a JSON object with an optional qualified table name; an empty object optimizes every table.

http
POST /optimize HTTP/1.1
Content-Type: application/json
Authorization: Bearer <token>

{"table":"app_db.articles"}

Content-Type must be application/json. When api.admin_token is non-empty, this endpoint requires an exact Authorization: Bearer <token> header. Without it, the endpoint returns 401 with WWW-Authenticate: Bearer and error_code: 7. The token comparison is constant-time.

Only GET /config, GET /replication/status, and POST /optimize use the token as an admission requirement. GET /health/ready, GET /health/detail, and GET /metrics check the same credential to decide whether to reveal protected replication data, but stay open to everyone.

Successful requests return 200 with { "status": "ok", "result": "OPTIMIZED ..." }. The body accepts only table; an unknown field or a non-string table is a 400 request error.

GET /tables/{identity}/ ​

Get a single document by its primary key.

Request:

http
GET /tables/app_db.threads/thread_12345 HTTP/1.1

Response (200 OK):

json
{
  "primary_key": "thread_12345",
  "filters": {
    "status": 1,
    "user_id": 42
  }
}

The response contains the primary key and configured filter fields. It does not include the original document text or a highlighted snippet; request highlight on POST /tables/{identity}/search when you need snippets.

Error Response (404 Not Found):

json
{
  "error": "Document not found",
  "error_code": 4004
}

GET /info ​

Server information and detailed statistics (Redis-style monitoring).

When to use /info

/info suits human reading and simple monitoring scripts that parse JSON. For continuous time-series monitoring with Prometheus and Grafana, use /metrics.

Request:

http
GET /info HTTP/1.1

Response (200 OK):

json
{
  "server": "MygramDB",
  "version": "1.12.0",
  "uptime_seconds": 3600,
  "total_requests": 15000,
  "total_commands_processed": 15000,
  "memory": {
    "used_memory_bytes": 524288000,
    "used_memory_human": "500.00 MB",
    "peak_memory_bytes": 629145600,
    "peak_memory_human": "600.00 MB",
    "used_memory_index": "400.00 MB",
    "used_memory_documents": "100.00 MB",
    "total_system_memory": 17179869184,
    "total_system_memory_human": "16.00 GB",
    "available_system_memory": 9126805504,
    "available_system_memory_human": "8.50 GB",
    "system_memory_usage_ratio": 0.47,
    "process_rss": 545259520,
    "process_rss_human": "520.00 MB",
    "process_rss_peak": 629145600,
    "process_rss_peak_human": "600.00 MB",
    "memory_health": "HEALTHY"
  },
  "index": {
    "total_documents": 1000000,
    "total_terms": 1500000,
    "total_postings": 5000000,
    "avg_postings_per_term": 3.33,
    "delta_encoded_lists": 1200000,
    "roaring_bitmap_lists": 300000
  },
  "tables": {
    "products": {
      "documents": 500000,
      "terms": 800000,
      "postings": 2500000,
      "ngram_size": 2,
      "memory_bytes": 262144000,
      "memory_human": "250.00 MB"
    },
    "users": {
      "documents": 500000,
      "terms": 700000,
      "postings": 2500000,
      "ngram_size": 1,
      "memory_bytes": 262144000,
      "memory_human": "250.00 MB"
    }
  }
}

Response Fields:

FieldDescription
serverServer name (MygramDB)
versionServer version
uptime_secondsServer uptime in seconds
total_requestsTotal number of requests processed
total_commands_processedTotal number of commands processed
Memory (Application)
memory.used_memory_bytesCurrent memory usage in bytes (index + documents)
memory.used_memory_humanHuman-readable current memory usage
memory.peak_memory_bytesPeak memory usage in bytes
memory.peak_memory_humanHuman-readable peak memory usage
memory.used_memory_indexMemory used by index
memory.used_memory_documentsMemory used by document store
Memory (System)
memory.total_system_memoryTotal physical RAM in bytes
memory.total_system_memory_humanHuman-readable total system memory
memory.available_system_memoryAvailable physical RAM in bytes
memory.available_system_memory_humanHuman-readable available memory
memory.system_memory_usage_ratioSystem-wide memory usage (0.0-1.0)
Memory (Process)
memory.process_rssProcess RSS (physical memory used) in bytes
memory.process_rss_humanHuman-readable process RSS
memory.process_rss_peakPeak RSS since process start in bytes
memory.process_rss_peak_humanHuman-readable peak RSS
Memory (Health)
memory.memory_healthMemory health status (HEALTHY/WARNING/CRITICAL/UNKNOWN)
Index (Aggregated)
index.total_documentsTotal number of documents across all tables
index.total_termsTotal number of unique terms
index.total_postingsTotal number of postings
index.avg_postings_per_termAverage postings per term
index.delta_encoded_listsNumber of posting lists using delta encoding
index.roaring_bitmap_listsNumber of posting lists using Roaring Bitmaps
Tables (Per-table)
tables.<name>.documentsNumber of documents in table
tables.<name>.termsNumber of terms in table
tables.<name>.postingsNumber of postings in table
tables.<name>.ngram_sizeN-gram size for table
tables.<name>.memory_bytesMemory usage for table in bytes
tables.<name>.memory_humanHuman-readable memory usage for table

Memory Health Status:

StatusMeaning
HEALTHYMore than 20% of system memory is available
WARNING10-20% of system memory is available
CRITICALLess than 10% of system memory is available (OPTIMIZE is rejected)
UNKNOWNThe status cannot be determined

Monitoring tools that read JSON can consume this endpoint directly.

GET /metrics ​

Prometheus metrics endpoint in Prometheus Exposition Format for monitoring and alerting.

What is the Prometheus Exposition Format?

It is the text format Prometheus scrapes. Beyond values it carries types such as Counter and Gauge plus labels, which is what makes the data usable as time series in tools like Grafana.

Request:

http
GET /metrics HTTP/1.1

Response (200 OK):

text
# HELP mygramdb_server_info MygramDB server information
# TYPE mygramdb_server_info gauge
mygramdb_server_info{version="1.12.0"} 1

# HELP mygramdb_server_uptime_seconds Server uptime in seconds
# TYPE mygramdb_server_uptime_seconds counter
mygramdb_server_uptime_seconds 3600

# HELP mygramdb_memory_used_bytes Current memory usage in bytes
# TYPE mygramdb_memory_used_bytes gauge
mygramdb_memory_used_bytes{type="index"} 419430400
mygramdb_memory_used_bytes{type="documents"} 104857600
mygramdb_memory_used_bytes{type="total"} 524288000

# HELP mygramdb_memory_health_status Memory health status (0=UNKNOWN, 1=HEALTHY, 2=WARNING, 3=CRITICAL)
# TYPE mygramdb_memory_health_status gauge
mygramdb_memory_health_status 1

# HELP mygramdb_index_documents_total Total number of documents in the index
# TYPE mygramdb_index_documents_total gauge
mygramdb_index_documents_total{table="products"} 500000
mygramdb_index_documents_total{table="users"} 500000

# HELP mygramdb_command_total Total number of commands executed by type
# TYPE mygramdb_command_total counter
mygramdb_command_total{command="search"} 10000
mygramdb_command_total{command="count"} 2000
mygramdb_command_total{command="get"} 3000

Content-Type: text/plain; version=0.0.4; charset=utf-8

Metric Categories:

CategoryDescription
Server MetricsServer version, uptime, commands processed
Command StatisticsCommand execution counters by type (search, count, get, info, etc.)
Memory MetricsApplication memory (index/documents), system memory, process RSS, health status
Index MetricsDocuments, terms, postings, optimization status (per-table with table label)
Client MetricsCurrent connections, total connections
Replication MetricsReplication status, events processed, operation counters (MySQL build only)

Metric Types:

  • Counter: Monotonically increasing values (e.g., mygramdb_command_total)
  • Gauge: Values that can increase or decrease (e.g., mygramdb_memory_used_bytes)

Prometheus Scrape Configuration:

yaml
scrape_configs:
  - job_name: 'mygramdb'
    scrape_interval: 15s
    authorization:
      type: Bearer
      credentials_file: /run/secrets/mygramdb_admin_token
    static_configs:
      - targets: ['localhost:8080']
        labels:
          environment: 'production'

When api.admin_token is configured, Prometheus must send the token to receive mygramdb_replication_* metrics. Store only the token in the file named by credentials_file and restrict access to it. Without accepted credentials, /metrics remains public but omits the complete replication metric family, including its HELP and TYPE lines.

Key Features:

  • Standard Prometheus format: Compatible with all Prometheus-based monitoring stacks
  • Multi-dimensional metrics: Uses labels for grouping (e.g., table, command, status)
  • Memory health tracking: Numeric status values for alerting (1=HEALTHY, 2=WARNING, 3=CRITICAL)
  • Per-table metrics: Index statistics broken down by table name
  • Backward compatible: Existing /info endpoint remains unchanged

Comparison with /info:

Feature/info/metrics
FormatJSONPrometheus text
Use caseGeneral monitoring, debuggingPrometheus/Grafana integration
Metric typesGeneric valuesTyped metrics (Counter/Gauge)
Multi-dimensionalLimitedFull label support
CompatibilityAny HTTP clientPrometheus ecosystem

Both endpoints include server status, memory health, and index statistics, but the available fields and detail differ. Replication series are available only from /metrics; when api.admin_token is configured, a valid Bearer token is required to receive them. Use /metrics for Prometheus integration and /info for general-purpose monitoring or human-readable output.

GET /health ​

Health check endpoint for load balancers and monitoring.

Request:

http
GET /health HTTP/1.1

Response (200 OK):

json
{
  "status": "ok",
  "timestamp": 1699000000
}

GET /health/live ​

Liveness probe. Returns 200 OK while the HTTP server process is running, even if the node is still loading or replication is degraded.

GET /health/ready ​

Readiness probe for traffic gating. Returns 200 OK only when all of these hold:

  • Initial data is loaded and the server is not loading.
  • No SYNC is running.
  • Configured replication is available.

Otherwise it returns 503 Service Unavailable. The JSON body includes data_initialized, loading, and status ("ready" or "not_ready"). Table read routes (search, count, facet, and document GET) also return 503 while their table is synchronizing.

/health, /health/live, and /health/ready are not rate limited, so a probe is not answered with 429. /health/detail remains rate limited because it builds a detailed snapshot.

Choosing between live and ready

On Kubernetes, wire /health/live to the liveness probe and /health/ready to the readiness probe. live answers "should this process be restarted"; ready answers "may search traffic be sent here".

GET /health/detail ​

Detailed monitoring snapshot. Returns 200 OK with "status": "healthy" or "status": "degraded". Use /health/ready, not this endpoint, for load balancer readiness decisions.

The route is open, but when api.admin_token is configured it omits current_gtid, processed_events, queue_size, crc_errors, and schema_incompatible unless the request has a valid Authorization: Bearer <token> header. /health/ready likewise omits replication_crc_errors and replication_schema_incompatible without accepted admin credentials.

/health/ready and /health/detail expose a description of the replication error code instead of raw MySQL error text. During SYNC, the binlog component reports a sync pause separately from a disconnected replication state. Every HTTP route, including /health, /health/live, and /health/ready, is subject to the CIDR allowlist.

GET /config ​

Current server configuration summary (sensitive values are omitted).

Request:

http
GET /config HTTP/1.1
Authorization: Bearer <token>

When api.admin_token is non-empty, /config requires an exact Authorization: Bearer <token> header. A missing or invalid token returns 401 Unauthorized, includes WWW-Authenticate: Bearer, and uses error_code: 7. If no admin token is configured, the route is open. If no configuration object is loaded, it returns 503 Service Unavailable with error_code: 6026.

Response (200 OK):

json
{
  "mysql": {
    "configured": true,
    "database_defined": true
  },
  "api": {
    "tcp": {
      "enabled": true
    },
    "http": {
      "enabled": true,
      "cors_enabled": false
    }
  },
  "network": {
    "allow_cidrs_configured": false
  },
  "replication": {
    "enable": true
  },
  "notes": "Sensitive configuration values are redacted over HTTP. Use CONFIG SHOW over a secured connection for details."
}

GET /replication/status ​

MySQL replication status (requires replication enabled).

Request:

http
GET /replication/status HTTP/1.1
Authorization: Bearer <token>

When api.admin_token is non-empty, /replication/status has the same bearer-token requirement and 401 response as /config; with no configured token it is open. In a MySQL-enabled build with no binlog reader, it returns 200 OK with {"enabled":false,"status":"not_configured"}. When a reader is present, the enabled field reflects whether replication is running, and status is "running", "stopped", or "failed". The full response includes replication counters and timestamps:

Response (200 OK):

json
{
  "enabled": true,
  "status": "running",
  "current_gtid": "3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5",
  "processed_events": 12345,
  "queue_size": 0,
  "crc_errors": 0,
  "schema_incompatible": false,
  "last_error_code": 0,
  "last_error": "",
  "last_applied_unixtime": 1756149998,
  "seconds_since_last_applied": 2
}

Response when replication is not configured in a MySQL-enabled build (200 OK):

json
{
  "enabled": false,
  "status": "not_configured"
}

Response in a build without MySQL support (503 Service Unavailable):

json
{
  "error": "MySQL replication not compiled",
  "error_code": 4
}

CORS Support ​

Set api.http.enable_cors: true to turn on CORS (Cross-Origin Resource Sharing) headers for browser clients, then specify the trusted origin via api.http.cors_allow_origin. When enable_cors is true, cors_allow_origin must not be empty: the configuration fails to load and the server does not start. MygramDB never falls back to a wildcard origin. Keep CORS disabled when the API is not accessed directly from browsers.

CORS Headers:

Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization

Usage Examples ​

The examples below use the bare threads identity, which works in a single-database deployment. With two or more databases, qualify it as app_db.threads (e.g. /tables/app_db.threads/search).

cURL ​

Search:

bash
curl -X POST http://localhost:8080/tables/threads/search \
  -H "Content-Type: application/json" \
  -d '{
    "q": "machine learning AND python",
    "mode": "boolean",
    "filters": {"status": 1},
    "limit": 10
  }'

Facet:

bash
curl -X POST http://localhost:8080/tables/threads/facet \
  -H "Content-Type: application/json" \
  -d '{
    "column": "category",
    "q": "machine learning",
    "filters": {"status": 1},
    "limit": 10
  }'

Get document:

bash
curl http://localhost:8080/tables/threads/thread_12345

Health check:

bash
curl http://localhost:8080/health

JavaScript (fetch) ​

javascript
// Search
const response = await fetch('http://localhost:8080/tables/threads/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    q: 'machine learning AND python',
    mode: 'boolean',
    filters: { status: 1 },
    limit: 10
  })
});

const data = await response.json();
console.log(`Found ${data.count} results`);
data.results.forEach(doc => {
  console.log(`Document ${doc.primary_key}`);
});

Python (requests) ​

python
import requests

# Search
response = requests.post('http://localhost:8080/tables/threads/search', json={
    'q': 'machine learning AND python',
    'mode': 'boolean',
    'filters': {'status': 1},
    'limit': 10
})

data = response.json()
print(f"Found {data['count']} results")
for doc in data['results']:
    print(f"Document {doc['primary_key']}")

Performance Considerations ​

  • Connection Pooling: Use HTTP keep-alive for better performance
  • Pagination: Use limit and offset for large result sets
  • Caching: Consider caching frequent queries at the application layer
  • Network Security: Use network.allow_cidrs to restrict access to trusted IP ranges

Error Handling ​

Handler-generated error responses follow this format:

json
{
  "error": "Error message description",
  "error_code": 3000
}

error_code is the machine-readable MygramDB error code. Branch on it when client behavior depends on the cause rather than matching the human-readable error string. An oversized request body is rejected with 413 Payload Too Large by the HTTP server before routing, so clients must rely on the status code rather than a JSON body or error_code for that response.

Invalid JSON, JSON nesting deeper than 32 levels, and malformed Boolean expressions return 400 with error_code: 3000. Other invalid fields can use a more specific code, such as 3006 for a filter condition.

HTTP Status Codes:

CodeDescription
200Success
400Bad Request (invalid input)
401Unauthorized (missing or invalid Bearer token for an administrative route such as /config, /replication/status, or /optimize)
403Forbidden (source IP is not allowed)
404Not Found (missing document: error_code: 4004; unresolved table identity, including a bare name under a multi-database configuration: 4007)
413Payload Too Large (request body exceeds api.http.max_body_bytes)
415Unsupported Media Type (a POST body sent without Content-Type: application/json; error_code: 6007)
429Too Many Requests (rate limit exceeded)
500Internal Server Error
503Service unavailable (error_code: 6026 when GET /config has no loaded configuration), feature not enabled, replication unavailable in a build without MySQL support (error_code: 4), or table synchronization in progress

Monitoring ​

The HTTP API has four endpoints for monitoring:

PurposeEndpointUse it for
Health checkGET /healthSimple check for load balancers
JSON statisticsGET /infoDetailed statistics for general monitoring tools
Prometheus metricsGET /metricsTime-series monitoring and alerting
Replication statusGET /replication/statusMySQL replication status

Monitoring Stack Integration ​

Prometheus + Grafana:

  1. Configure Prometheus to scrape /metrics endpoint
  2. Import Grafana dashboards for MygramDB visualization
  3. Set up alerts based on memory health, query latency, and replication lag

Other Monitoring Tools:

  • Datadog/New Relic: Parse /info JSON endpoint
  • Zabbix: HTTP agent checks on /health and /info
  • Nagios/Icinga: Check scripts using /health endpoint

See Also ​