Skip to main content

What is the Query API?

The Grain Query API allows you to programmatically access your analytics data using simple HTTP requests. While the Grain Dashboard provides a powerful visual interface for exploring your data, the Query API enables you to:
  • Build custom analytics dashboards tailored to your specific needs
  • Export data for external analysis or reporting
  • Integrate analytics into your own applications and workflows
  • Automate data processing with scheduled scripts and pipelines
Plan Requirement: The Query API requires a Builder plan or higher. Free plan users can upgrade at grainql.com/settings/billing.

Quick Start

Get up and running with the Query API in 3 steps:
1

Create an API Key

Navigate to your Dashboard Settings and create a secret with Query API permissions:
  1. Go to Dashboard → Settings → Authentication
  2. Click Generate New Secret
  3. Enter a name like “Query API Key”
  4. Check the “Query API” permission
  5. Copy the generated secret (you won’t see it again!)
Authentication Strategy: The Query API authentication is independent of your SDK authentication strategy. You can use Query API keys regardless of whether your SDK is set to NONE, SERVER_SIDE, or JWT authentication.
Store your API key securely in environment variables. Never commit it to version control.
2

Make Your First Request

Query your events with a simple HTTP request:
This returns the last 10 page view events from January 2024.
3

Explore Your Data

Use the Query API to build custom analytics:

When to Use the Query API

✅ Perfect for:

  • Custom Dashboards: Build analytics views that match your specific workflow
  • Data Exports: Extract data for external analysis, reporting, or compliance
  • Integrations: Connect Grain data to other tools (Slack, email, BI platforms)
  • Automation: Scheduled reports, alerts, and data processing pipelines
  • Advanced Analysis: Complex queries that go beyond the dashboard interface

❌ Consider the Dashboard instead for:

  • Exploratory Analysis: The dashboard’s visual interface is great for discovering insights
  • One-off Queries: Quick questions that don’t need automation
  • Real-time Monitoring: The dashboard provides live event streams

Understanding Your Data

Before using the Query API, it helps to understand how your analytics data is structured:

Event Structure

Every event in Grain has this structure:

Key Fields

  • eventName: The type of event (e.g., “page_viewed”, “purchase_completed”)
  • userId: The user who performed the action (set via grain.identify() or grain.setUserId())
  • eventTs: When the event occurred (ISO 8601 timestamp)
  • properties: Custom data you included when tracking the event
  • eventDate: Date portion for efficient querying
  • insertId: Unique identifier for the event

Property Paths

When filtering events, you can query:
  • Event properties: properties.price, properties.category
  • Event metadata: eventName, eventTs, userId
  • Date fields: Use after and before parameters for date ranges

Common Use Cases

1. Custom Analytics Dashboard

Build a dashboard that shows exactly what you need:

2. Data Export for Analysis

Export data for external analysis:

3. User Behavior Analysis

Analyze user journeys and behavior patterns:

4. Real-time Monitoring

Set up monitoring and alerts:

Integration with Dashboard Features

The Query API works seamlessly with your existing Grain dashboard:

Matrices Integration

Use the Query API to build custom matrices that complement the Matrices page:

Tracks Integration

Query data that powers your Tracks funnels:

Rate Limits and Performance

Plan Limits

Rate Limit Planning: These limits are designed for typical analytics use cases. For high-frequency applications, real-time dashboards, or bulk data exports, consider our custom plans with higher limits.

Best Practices

1. Use the Count Endpoint for Aggregations
2. Implement Pagination for Large Datasets
3. Handle Rate Limits Gracefully
Rate Limit Considerations: With the current rate limits (2-100 requests per minute), consider implementing request queuing or batching for high-frequency applications. For production applications with higher throughput needs, contact us about custom plans.

Security Best Practices

1. Never Expose API Keys in Client Code

Don’t do this:
Do this instead:

2. Use Environment Variables

3. Rotate Keys Regularly

  1. Generate a new API key in the dashboard
  2. Update your application with the new key
  3. Deploy the changes
  4. Revoke the old key after confirming the new one works

Next Steps

Now that you understand the basics, explore the detailed API reference:

API Reference

Complete endpoint documentation

Filter Reference

All filter operators and examples

Custom Dashboard

Build a custom analytics dashboard

Data Export

Export data for analysis

Custom Plans

Need higher rate limits or have specific usage requirements? We offer custom plans tailored to your needs:
  • Higher rate limits: Custom requests per minute/day limits
  • Dedicated support: Priority support and SLA guarantees
  • Custom features: Tailored analytics features and integrations
  • Volume discounts: Competitive pricing for high-volume usage
Contact us at support@grainql.com to discuss your requirements and get a custom quote.

Support