Introduction
The framework automatically creates an API for you based on the queries you have defined in your project. This means that you can easily expose your data to any other app or service. In this tutorial, we will learn to:Prerequisites
Before you begin, make sure you followed the installation guide to install Latitude.1. Create a new data app
To create a new data app, run the following command in your terminal:views
directory from your project.
2. Add a data source
To add a data source to your app, you’ll need to create a new file in thesources
directory.
For this guide, we’ll use the CSV that comes with the sample project, but you can replace that with any other CSV or connect to a live database following these instructions.
Go ahead and download the sample CSV and save it in the queries
directory as source.csv
.
Download sample CSV
Visit the page and hit
Cmd + S
or Ctrl + S
to save the file3. Write your queries
Now that you have a data source, you can write your first query. Create a new file namedsample.sql
in the queries
directory and write the following query:
sample.sql
source.csv
file using our default DuckDB adapter (you don’t need to configure anything else to get this working).
Next, let’s create a secondary query to aggregate some data. Create a new file named sample_agg.sql
in the queries
directory and write the following query:
sample_agg.sql
ref
function is used to interpolate the sample.sql
query, and use its results as the input for this query. Latitude allows you to chain queries together very easily, find out more about this in the queries reference.
To check that your queries are working as expected, you can run them locally using the latitude run
command. For example:
/queries/sample_agg.sql
query once and display the results in your terminal.
Learn more about latitude run
in the CLI reference.
4. Accessing the API endpoints
Now that you have your queries set up, you can access the API endpoints. The Latitude automatically creates an API for you based on the file structure you have defined in your project. To access the API, you can use any HTTP client, such ascurl
, Postman, or even your browser. For example, to access the /api/queries/sample_agg
endpoint, you can run the following command in your terminal:
sample_agg.sql
query in JSON format.
4.1 Passing parameters to your queries
You can also pass parameters to your queries by adding them to the URL. Let’s edit oursample_agg.sql
file to support a year
parameter. Replace the contents of the file with the following and save it:
sample_agg.sql
year
parameter to the /api/queries/sample_agg
endpoint. For example, to get the results for the year 2020, you can run the following command in your terminal:
sample_agg.sql
query for the year 2020 in JSON format.
4.2 Downloading query results as CSV
You can pass a special__download
boolean parameter to the URL to download
the results as a CSV file. For example, to download the results of the
sample_agg.sql
query as a CSV file, you can run the following command in your
terminal: