Get API
Introduction
FrontQL is a lightweight front-end querying language designed to streamline client-server communication for web applications. It simplifies data retrieval by allowing clients to request exactly what they need from the server. One of the key features of FrontQL is its GET API, which enables clients to fetch data from the server using HTTP GET requests.
The GET API of FrontQL is based on a structured query language that is both powerful and easy to understand. When a client makes a GET request to a FrontQL-enabled server, it includes a query parameter that specifies the fields and objects to be returned. The server then processes this query and returns the requested data in a structured format, typically JSON, which can be easily consumed by the client application.
By using the GET API of FrontQL, developers can reduce the amount of data transferred over the network, minimize the need for multiple API endpoints, and ensure that applications are more performant and scalable.
Example
Here’s a simple example of a FrontQL GET API request:
async function getUsers() { const response = await Api.get("/users");
return response;}
async function getUsers(){ const url = `${BASE_URL}/users`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}
Output
This example fetches all users from the server and returns them in a JSON format.
{ "err": false, "count": 2, "result": [ { "id": 1, "name": "John Doe", "email": "9kUjZ@example.com", "created_at": "2022-01-01 00:00:00.000", "updated_at": "2022-01-01 00:00:00.000" }, { "id": 2, "name": "Jane Doe", "email": "9kUjZ@example.com", "created_at": "2022-01-01 00:00:00.000", "updated_at": "2022-01-01 00:00:00.000" } ]}
Parameters
page
: stringsort
: stringjoins
: stringhidden
: stringfields
: stringfilter
: stringsearch
: string
page
- It is required for pagination.
- There are two parameters in the
page
parameter:page
andlimit
. page
is the page number andlimit
is the number of items per page.page
andlimit
are separated by a comma.- Example:
async function getUsers() { const response = await Api.get("/users", { page: "1,15", // page = 1, limit = 15 });
return response;}
async function getUsers(){ const url = `${BASE_URL}/users?page=1,15`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}
sort
- It is required for sorting.
- Example:
async function getUsers() { const response = await Api.get("/users", { sort: "-created_at", });
return response;}
async function getUsers(){ const url = `${BASE_URL}/users?sort=-created_at`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}
joins
- It is required for joining two or more collections.
- Example:
async function getUsersPosts() { const response = await Api.get("/posts", { join: "user:users", });
return response;}
async function getUsers(){ const url = `${BASE_URL}/users`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", collections: "user:users", // join becomes collections in fetch app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}
hidden
- It is required for hiding fields which should not be returned.
- Example:
async function getUsers() { const response = await Api.get("/users", { hidden: "password", });
return response;}
async function getUsers(){ const url = `${BASE_URL}/users`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", hidden: "password", app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}
fields
- It is required for specifying the fields to be returned.
- Example:
async function getUsers() { const response = await Api.get("/users", { fields: "id,name,email", });
return response;}
async function getUsers(){ const url = `${BASE_URL}/users`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", fields: "id,name,email", app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}
filter
- It is required for filtering.
- If the filter changes, the token has to be regenerated.
- It is passed in the headers of the request.
- Example:
async function getUsers() { const response = await Api.get("/users", { filter: "name:John", });
return response;}
async function getUsers(){ const url = `${BASE_URL}/users`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", filter: "name:John", app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}
search
- It is required for searching.
- If search changes, no need to regenerate the token.
- It is passed to the params of the request.
- Example:
async function getUsers() { const response = await Api.get("/users", { search: "name:John", });
return response;}
async function getUsers(){ const url = `${BASE_URL}/users?search=name:John`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}
Single Data Fetch
- It is possible to fetch a single object from the server.
- Simply add the ID of the object after the
DB_NAME
in the URL with a/
. - Example:
async function getUser() { const response = await Api.get("/users/1");
return response;}
async function getUsers(){ const url = `${BASE_URL}/users/1`; // Replace BASE_URL with the base URL of API server const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", app: DATABASE, // Replace DATABASE with the name of your database token: TOKEN, // Replace TOKEN with the token // OR // Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token } });
const data = await response.json(); return data;}