---
title: Run a BQL query
summary: Send a BQL query to one or more versions in JavaScript, Java, .NET, PHP or over HTTP, and read the records that come back.
order: 10
languages: [javascript, java, csharp, php]
terms: [qid, vid, version, scope]
see-also: [reference/bql, how-to/query/count-results, how-to/query/use-query-parameters, how-to/query/query-multiple-models, tutorials/bql, tutorials/openapi/getting-started]
---

# Run a BQL query

For every integration that reads model data. A query is a string in BQL ([[page:reference/bql]]), the models are [[term:version|version]] UUIDs, and the answer is a list of records whose fields are the features you asked for.

## Before you start

- A login with the `query:run` [[term:scope|scope]] and read rights on the versions ([[page:how-to/auth/log-in-with-an-api-token]]).
- The version UUIDs ([[page:how-to/models/list-the-tree]]); the demo models of your instance are the placeholders below.
- A query. `GET qid, type, attribute.Name WHERE type = IFCWALL` is a good first one; the cheat sheet is [[page:reference/bql]].

## 1. Run the query

```code-group {#query} classic
```javascript
const objects = await client.bim.query([BIM_WORKS_FIRST_MODEL],
	"GET qid, type, attribute.Name WHERE type = IFCWALL");
for (const object of objects) {
	console.log(object.qid.toString(), object.type, object.attribute.Name);
}
```
```java
import java.util.UUID;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

ArrayNode objects = client.bim.query("GET qid, type, attribute.Name WHERE type = IFCWALL",
		UUID.fromString(BIM_WORKS_FIRST_MODEL));
for (JsonNode object : objects) {
	System.out.println(object.get("qid").asText() + " " + object.get("type").asText() + " " + object.path("attribute").path("Name").asText());
}
```
```csharp
using System.Text.Json;

JsonElement objects = client.Bim.Query(Guid.Parse(BIM_WORKS_FIRST_MODEL),
    "GET qid, type, attribute.Name WHERE type = IFCWALL");
foreach (JsonElement obj in objects.EnumerateArray())
{
    Console.WriteLine($"{obj.GetProperty("qid")} {obj.GetProperty("type")} {obj.GetProperty("attribute").GetProperty("Name")}");
}
```
```

```sdk-example
operation: Query
body: |
  {
    "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}],
    "query": "GET qid, type, attribute.Name WHERE type = IFCWALL"
  }
```

The methods: `query(models, query)` on [[js:@bimworks/jssdk/Bim#query|Bim.query]], `BimApi.query(query, versionUuids…)` on [[java:nl.bimbase.bimworks.client.apis.BimApi#query|BimApi.query]], `Bim.Query(versionUuid, query)` on [[dotnet:BIM.works_SDK.Bim#Query|Bim.Query]] and `Bim.Query2(versionUuids, query)` on [[dotnet:BIM.works_SDK.Bim.Query2(System.Collections.Generic.ISet{System.Guid},System.String)|Bim.Query2]], `IFCApi::query` in [[page:reference/sdk/php]]. All of them call the same server task; the SDKs time out after one minute (Java) or when the transport does. Over HTTP the operation is [[openapi:Query]]:

```try-it
operation: Query
method: POST
path: /openapi/ifc/Query
body: |
  {
    "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}],
    "query": "GET qid, type, attribute.Name WHERE type = IFCWALL"
  }
```

With `"format": "ifc"` or `"ifcxml"` in the body the same operation answers with the matching objects as an IFC or ifcXML file instead of JSON; `count`, `qids`, `csv` and `xlsx` give the number of objects, the selection and a table ([[bql:output]]).

## 2. Read the records

One record per matching object, with one field per `GET` column:

```json {#record}
{
  "qid": "797e993c-a87f-4aa7-9587-c59d30ec5c4e-1316",
  "type": "IFCWALL",
  "attribute": {"Name": "Basic Wall:Interior - 165 Partition:339437"}
}
```

- Dotted features become nested objects (`attribute.Name` is `record.attribute.Name`); `AS` aliases become top-level fields; an aggregate keeps its expression as the field name (`COUNT(qid)`).
- `qid` identifies the object: the version's [[term:vid|vid]], a dash and the object id, see [[term:qid]]. It is present when you `GET qid`; a `GROUP BY` query has no qid unless you `COLLECT(qid)`. Java, .NET, PHP and HTTP give it as that string; the JavaScript SDK gives a `Qid` object with `vid`, `oid` and `toString()`, and its `queryQids` variant returns a `QidSet` for the viewers.
- `type` is the IFC entity name in upper case when you asked for it.
- A feature an object lacks is absent from its record; `= null` in the `WHERE` clause finds those objects.
- The JavaScript SDK unpacks the server's compact wire format (`compression: 1`, in which version ids and types are sent once and referenced by index) before it returns; over HTTP you get plain records unless you ask for `compression: 1` yourself.

## 3. Cache what you repeat

The JavaScript `query` takes a third argument, `useCache`: with `true`, the client keeps the result in memory keyed by models, query and parameters, and answers the same call again without a round trip. The cache lives as long as the client and is never invalidated by uploads, so use it for a page that re-asks the same question, not for a long-running application. The server caches too, per model and query text, which is why an identical query is fast the second time in every SDK.

## 4. Read the errors

A query the server cannot parse fails with [[error:SYNTAX_ERROR]]; an unknown entity type with [[error:UNKNOWN_TYPE]]; a version you may not read with [[error:AuthorizationError.NO_READ_RIGHTS]] and one that does not exist with [[error:MODEL_NOT_FOUND]]; a missing scope with [[error:NON_AUTHORIZED_SCOPE]]. In JavaScript the promise rejects with the error object (`domain`, `type`, `code`), Java throws `BimWorksException` with `getErrorCode()`, .NET throws `BimWorksException`, PHP an `ApiException`, and HTTP answers `400`, `403` or `404` with the `ErrorResponse` body. All codes are on [[page:reference/errors]].

## Check

`GET type, COUNT(qid) GROUP BY type` on the first demo model returns one record per entity type with its count, and the sum of the counts equals `queryCount` of `GET qid` ([[page:how-to/query/count-results]]).
