opensheet
repository·main·Indexed 21 days ago
https://github.com/benborgers/opensheetA hosted API and Bun server that converts Google Sheets into JSON data, allowing spreadsheets to be used as a data source for web applications. It supports accessing tabs by name or index, raw data mode via query parameters, and 30-second response caching. The project can be self-hosted with Redis, PostgreSQL, and a Google Sheets API key.
What's inside opensheet
- API responses are cached for 30 seconds to improve performance and prevent hitting Google Sheets' rate limits. Note that new edits made to a Google Sheet may take up to 30 seconds to appear in the API response.
Use the opensheet API to get Google Sheets as JSON
mainThe opensheet API converts Google Sheets into JSON data. To use it, ensure your Google Sheet meets these requirements:
- Header Row: The first row must contain headers.
- Link Sharing: Link sharing must be enabled so that anyone with the link can view the sheet.
URL Format
https://opensheet.elk.sh/<spreadsheet_id>/<tab_name_or_number>Accessing Tabs
- By Name: Use the tab name (e.g.,
Test+Sheet). - By Index: Use the tab number (starting at
1).
Raw Data Mode
By default, the API returns formatted data (numbers, dates, etc.). To get the underlying raw values, append the
?raw=truequery parameter.https://opensheet.elk.sh/1o5t26He2DzTweYeleXOGiDjlU4Jkx896f95VUHVgS8U/Test+Sheet // To get raw, unformatted data: https://opensheet.elk.sh/1o5t26He2DzTweYeleXOGiDjlU4Jkx896f95VUHVgS8U/Test+Sheet?raw=true // To get the first tab by index: https://opensheet.elk.sh/1o5t26He2DzTweYeleXOGiDjlU4Jkx896f95VUHVgS8U/1Run opensheet in local development mode
mainTo start the opensheet server for local development using Bun, run the following command:
bun run devSelf-host opensheet
mainIf you wish to host your own instance of opensheet, note that it is a Bun server. You will need to provide the following infrastructure and configuration:
Requirements
- Redis: Used for caching.
- PostgreSQL: Used for analytics.
- Google Sheets API Key:
- Create a project in the Google Cloud Console.
- Enable the "Google Sheets API".
- Create an API key under "Credentials".
Environment Variables
Set the following variable in your environment:
GOOGLE_API_KEY
Fetch Google Sheet data via OpenSheet API
mainOpenSheet provides a RESTful interface to fetch Google Sheets data as JSON. The API converts spreadsheet rows into an array of objects where the keys are derived from the first row (the header row) of the sheet.
URL Structure
/:id/:sheetid: The Google Spreadsheet ID.sheet: Either the name of the sheet (string) or the index of the sheet (1-based integer).
Query Parameters
Parameter Type Description rawtrueorfalseIf set to true, the API uses Google'sUNFORMATTED_VALUErender option, returning raw data instead of formatted strings.Response Format
Returns a JSON array of objects:
[ { "Header1": "Value1", "Header2": "Value2" }, { "Header1": "Value3", "Header2": "Value4" } ]// Example: Fetching sheet index 1 with raw values // URL: https://your-opensheet-instance.com/SPREADSHEET_ID/1?raw=true // Expected Response: [ { "Name": "John Doe", "Email": "john@example.com" }, { "Name": "Jane Smith", "Email": "jane@example.com" } ]Handle OpenSheet API errors
mainIf a request fails (e.g., invalid sheet index, invalid query parameters, or Google API errors), OpenSheet returns a JSON error response with a non-200 status code.
Error Response Schema
{ "error": "Error message string", "documentation": "https://github.com/benborgers/opensheet#readme" }{ "error": "For this API, sheet numbers start at 1", "documentation": "https://github.com/benborgers/opensheet#readme" }