API Reference

Access data from external sources. Datasquirel is platform-agnostic so all you need is a HTTP request with the right credentials. Our NPM module abstracts this aspect for ease of use when using node.

Dotted image background

Overview

All data stored in your databases can be accessed through our integrated API. There are two sets of API keys, each with different scope. If you want to just fetch data use the Read Only API key. If you want to have full access to every aspect of your account, use the Full Access API key. Learn how to add API keys here.


Getting started

After you have your API keys, you can start making calls using our API integration. We have an NPM module for node projects, but you only need a HTTPS client to make the calls.

npm install datasquirel


Fetching Data using the get method

The get method only retrieves data. It cannot be used to write or update data. It was created to be the fastest way to access your data, that explains the key length. You can get data using the npm module or via any HTTP request.

  • Using CURL:

    CURL  
        https://datasquirel.com/api/query/get?db=db_name&query=SELECT+*+FROM+table_name 
    -H 
        "Authorization:READ_ONLY_API_KEY";Content-Type:application/json
  • Using node module

    const datasquirel = require("datasquirel");
    
    datasquirel
        .get({
            db: "test",
            key: process.env.DATASQUIREL_READ_ONLY_KEY,
            query: "SELECT title, slug, body FROM blog_posts",
        })
        .then((response) => {
            console.log(response);
        });

The response from this method returns a JSON payload with two fields: success: which could be either true or false, and payload: which is an array of values when the operation is successfull, or anything from null to a string to an object with an error message. This is a sample of a successful response object:

{
    success: true,
    payload: [
        {
            id: 1,
            title: "Hello World",
            slug: "hello-world",
            body: "This is a test blog post.",
        },
    ],
}

A failed response could return any of three results:

  • A null payload.

    {
        success: false,
        payload: null
    }

  • An error string payload

    {
        success: false,
        payload: "ERROR: no such table as 'blog_posts'"
    }
  • An object payload containing an error field

    {
        success: false,
        payload: { error: "MYSQL ERROR: syntax error in your sql" }
    }

In each case, the success key refurns a value of false , meaning the query failed.


Using the post method

The post method contains the full spectrum of CRUD operations. And it works *only with the Full Access API key. The read only API key will not work for post methods.

  • Using CURL
    CURL --json
        '{ 
            "database": "social_network",
            "query": "UPDATE users SET name = 'John' WHERE id = 1"
        }'
        https://datasquirel.com/api/query/post 
    -H 
        "Authorization:FULL_ACCESS_API_KEY"`

  • Using our npm module

    const datasquirel = require("datasquirel");
    
    datasquirel
        .post({
            database: "social_network",
            key: process.env.FULL_ACCESS_API_KEY,
            query: "UPDATE users SET name = 'John' WHERE id = 1",
        })
        .then((response) => {
            console.log(response);
        });

The process yeilds simalar results, but with a slight difference: for operations like insert and update, the success field yeilds true while the payload field yeilds an object containing fields like 

{
    success: true,
    payload: {
        serverStatus: 37,
        affectedRows: 1,
    }
}

The post method can also take an object as the query instead of a string. Example:

const datasquirel = require("datasquirel");

datasquirel
    .post({
        database: "social_network",
        key: process.env.FULL_ACCESS_API_KEY,
        query: {
            action: "update",
            table: "users",
            data: {
                name: "John",
            },
            identifierColumnName: "id",
            identifierValue: 1,
        },
    })
    .then((response) => {
        console.log(response);
    });

This yields the exact same result as before. Learn more about the post method here.