---
title: Use query parameters
summary: Write $name placeholders in a query and bind their values at call time, instead of building query strings from user input.
order: 30
languages: [javascript, java]
terms: [qid, version]
see-also: [how-to/query/run-a-bql-query, how-to/query/count-results, reference/bql, tutorials/bql]
---

# Use query parameters

For an application whose queries depend on user input: a type picked from a list, a threshold from a slider, a selection from the viewer. A parameter is `$` followed by a name, written where a literal would be; the values travel separately as a `parameters` object and are bound by the server. No quoting, no escaping, and the same query text every time, which also keeps the server's query cache useful.

## Before you start

- A login with the `query:run` scope and the version UUIDs ([[page:how-to/query/run-a-bql-query]]).

## 1. Write the query with placeholders

A parameter may replace a literal in a comparison (`= $x`, `> $min`, `BETWEEN $from AND $to`, `CONTAINS $text`), a list in `IN` (`type IN $types`, `qid IN $selection`) and the argument of `CLASHES_WITH`. Names are identifiers; a query may use one parameter several times.

```bql {#query}
GET qid, attribute.Name, calculated.quantity.volume UNIT m3
WHERE type IN $types AND calculated.quantity.volume > $minVolume
```

## 2. Bind the values when you call

Over HTTP, [[openapi:Query]] takes the values as `parameters`, an object of name to value:

```try-it
operation: Query
method: POST
path: /openapi/ifc/Query
body: |
  {
    "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}],
    "query": "GET qid, attribute.Name, calculated.quantity.volume UNIT m3 WHERE type IN $types AND calculated.quantity.volume > $minVolume",
    "parameters": {"types": ["IFCWALL", "IFCSLAB"], "minVolume": 2}
  }
```

Values are strings, numbers, booleans or arrays of those. `parameters` works with every format of the operation except `ifc` and `ifcxml`, so `"format": "count"` counts with them ([[page:how-to/query/count-results]]).

In JavaScript the `parameters` object is the fifth argument of `query` and the fourth of `queryCount` ([[js:@bimworks/jssdk/Bim|Bim]]), and `QueryHelper.toParameterValues` converts a `QidSet` from the viewer into a value for `qid IN $selection`:

```javascript {#bind}
const query = "GET qid, attribute.Name, calculated.quantity.volume UNIT m3 WHERE type IN $types AND calculated.quantity.volume > $minVolume";
const objects = await client.bim.query([BIM_WORKS_FIRST_MODEL], query, false, false, {
	types: ["IFCWALL", "IFCSLAB"],
	minVolume: 2
});
```

The Java SDK's `BimApi.query`, the .NET SDK and the PHP client have no parameters argument in this release: call the HTTP operation from those languages.

## 3. Read the errors

A placeholder without a value fails with [[error:MISSING_PARAMETER]], and a value of the wrong kind (a string for a number, a scalar for `IN`) with [[error:PARAMETER_TYPE_MISMATCH]]. The `parameters` field must be an object of name to value; anything else is a type mismatch too.

```note
`USING` is not a parameter mechanism. It is the clause that sets the units a query converts to (`USING LENGTH_UNIT = mm VOLUME_UNIT = m3`), see [[bql:using]]; the `UNIT` keyword on a single column does the same for that column.
```

## Check

Bind `minVolume: 2`, then `minVolume: 2000`: the second call returns fewer (or no) objects with the same query text. Leave `types` out: the call fails with [[error:MISSING_PARAMETER]].
