Search Constraints On Get Requests

I really need help adding Search Constraints so that I can limit the results I get from my GET request through the bubble data API. I seem to be adding query parameters incorrectly. I am writing a nodejs server that talks with the bubble database to get the data.

I am currently following the quickstart tutorial but want to add some search constraints to the request. Show me how please!

Here is the code I have right now. I define my constraints, try to make a valid URL with the constraints embedded in the URL and then send of the request. I am not getting the right data back. I am still getting ALL entries rather than the slimmed down amount I should be.

// Make constraints
let constraints = [
  {
    "key": "type",
    "constraint_type": "equals",
    "value": workoutType
  }
];

// Make URL with constraints
let constraintsStringified = JSON.stringify(constraints);
let queryStringStringified = querystring.stringify('constraints:' + constraintsStringified);
let url = "fitext.bubbleapps.io/version-test/api/1.1/obj/workout?";
let completeURL = url + queryStringStringified;
console.log(completeURL);
const options = new URL(completeURL);

// Make http request.
const req = https.request(options, res => {
  let data = "";
  res.on("data", d => {
    data += d;
  });

  res.on("end", () => {
    var jsonObj = JSON.parse(data);
    console.log(jsonObj)
    var workoutDescription = jsonObj.response.results[0].description;
    workoutDescription = workoutDescription.substring(0, 1550);
    callback(workoutDescription);
  });
});

req.on("error", error => {
  console.error(error);
});

req.end();

I have noted that same question has been asked several times with no response leave alone answers. I am struggling with the same. First of all, hope you have checked these guidelines here Introduction - Bubble Docs.

I am also looking at an example from network logs of dropsource that is powered by Bubble. See below screenshot.

For my case, I am using a axios nodeJs module but i don’t seem to get the correct way of dealing with bubble query parameters. I have also tried with postman but nothing. Maybe someone, @keith, @seanhoots, @copilot @vini_brito @sudsy

Here is my code:

const axios = require(“axios”);
// Make an HTTP GET request using axios
const resp = await axios({
method: “GET”,
url: ‘https://delicacy.bubbleapps.io/version-test/api/1.1/obj/county’,
params: (params) => {
constraints:[{“key”:“countyName”,“constraint_type”:“equals”,“value”:“Bomet”}];
}

//query: {constraints=[{“key”:“countyName”,“constraint_type”:“equals”,“value”:“Bomet”},]}

});

Hi both,
Constraints can be tricky, I wrote an example in this post:

Wrote a couple of examples below (with axios).

const axios = require('axios')

let token = 'my-token-here'
let endpoint = 'https://my-app.domain.com'
let id = '12341415x2341324'
let thing = 'customer'

const config = {
    headers: { Authorization: `Bearer ${token}` }
};


// GET ITEM BY ID
async function getThingById() {

    let url = `${endpoint}/version-test/api/1.1/obj/${thing}/${id}`
    //will look like: https://my-app.domain.com/version-test/api/1.1/obj/customer/158523442008x158523442008

    let res = await axios.get(url, config)
    let item = res.data
    console.log(item)
}

// GET ALL ITEMS OF A THING TYPE
async function getThings() {

    let url = `${endpoint}/version-test/api/1.1/obj/${thing}`
    //will look like: https://my-app.domain.com/version-test/api/1.1/obj/customer

    let res = await axios.get(url, config)
    let items = res.data
    console.log(items.response.results)
}

// GET THINGS WITH CONSTRAINT
async function getThingsWithConstraints() {

    let constraints = [
        {
            "key": "customername",
            "constraint_type": "text contains",
            "value": "John Doe"
        }
    ]

    let url = `${endpoint}/version-test/api/1.1/obj/${thing}?constraints=${JSON.stringify(constraints)}`
    //will look like: https://my-app.domain.com/version-test/api/1.1/obj/customer?constraints=[{"key":"customername","constraint_type":"text contains","value":"John Doe"}]

    let res = await axios.get(url, config)
    let items = res.data
    console.log(items.response.results)
}
1 Like

Thank you Simon. Thank you so much. Basically it looks like you have simply added the constraint to the URL directly. Will look at it closely and get back if i have further questions.

Exactly. But if you look at the screenshot you provided, its the same that dropsource does. “Query String Parameters” is the parameters that you provide in the url string itself. Every parameter is divided by a & sign, so that the url looks like:

https://app.com?constraints=[{CONSTRAINTS-HERE}]&decending=true...

In the top of the screenshot you see the full request-url. Here the string is url-encoded. %22 is the same as a ", so %22key%22 is url-encoded for: "key".

You can test with postman and you bubble-app or requestbin where you can make disposable urls to test your get requests from postman.

My tests with postman were working fine. Converting parameters into JSON string is what I and probably many others were missing.

I really appreciate for your detailed reply

I keep getting an error saying it can’t be parsed with JSON. I followed the exact URL from your post.
https://YOUR-APP.com/version-test/api/1.1/obj/jedissscing?constraints=[{“key”:“jobid”,“constraint_type”:“equals”,“value”:“1234567”}]

2 Likes

I got it working. For some reason when I copied URL from @simon7 other post here I was able to get it working.

Great. It can definately take a couple of tries to get working :smiley:

Is there a list of all possible constraint types for API calls somewhere?

EDIT: found it, https://bubble.io/reference#API.get_api.search_call.search_constraints

1 Like

The link https://bubble.io/reference#API.get_api.search_call.search_constraints is not working anymore. Does someone have a list of constraints?

Found new link: Data API - Bubble Docs
It’s very bad that constraints does not have following types:

  • greater than or equals
  • less than or equals
  • between
  • contains list
    etc