Skip to main content

API saved queries

Overview

Cinchy queries are automatically available as REST APIs to allow for external application integration.

To use your query programatically, you must call the Saved Query API endpoint using the specific REST API URL associated with your query, along with some form of authorization (either a Bearer Token or a PAT).

Find the REST API URL

The REST API URL is used in the Saved Query endpoint when passing through your API.

  1. The REST API URL can be found in two places:

    1. In the "REST API URL" column of the [Cinchy].[Saved Queries] table. (Cinchy v5.10+)
    2. From the "REST API" button on the Execute Query screen.

    REST API Button

    The URL contains the following parameters:

NameData TypeDescriptionExample
[Cinchy Web URL]stringThe URL for your Cinchy platform.Cinchy.net
[API Version]numberCinchy v5.10+ The version code associated with your query. This can be found in the Saved Queries table.1.0
[Domain]stringThe Domain associated with your saved query.Product
[Query Name]stringThe name of your query.Product Documentation Request

API Parameters

Use the below parameters to configure your API request.

v5.10+: GET: Https://[Cinchy Web URL]/API/v[API Version]/[Domain]/[Query Name]

Older versions GET: Https://<Cinchy Web URL>/API/MyDomain/MyQuery

Query Parameters

NameData TypeDescription
WrapSingleRecordInArrayBooleanDefault is true. Add this parameter and set to false if you want single record results returned as an object instead of within an array.
@paramstringIf you have parameters in your query, you pass them indirectly as query parameters.
CompressJSONBooleanDefault is true. Add this parameter and set to false if you want the JSON that's returned to be expanded rather than having the schema being returned separately.

Header Parameters

NameData TypeDescription
AuthorizationstringBearer <access_token>. The access token can be either a Bearer token or Personal Access token. See Authentication for details.

Responses

200: The request has successfully returned the record set.

< HTTP/2 200
< cache-control: private, s-maxage=0
< content-type: application/json; charset=utf-8
< server: Microsoft-IIS/10.0
< x-aspnetmvc-version: 5.2
< access-control-allow-origin: *
< x-aspnet-version: 4.0.30319
< x-powered-by: ASP.NET
< date: Wed, 1 Aug 2020 17:40:13 GMT
< content-length: 2985

400: The request couldn't be understood, the client is sending a request with incomplete data, poorly constructed data or invalid data.**

Optional Validation Logic: To validate query business / control conditional logic failure can be added at the beginning of the API which can intentionally generate a 400 error code (using RAISERROR) and stopping (using RETURN) the API. If there is no RETURN the errors will accumulate and will be provided at the end of running the API.

An attribute (X-Cinchy Error) will be returned in the HTTP header with the custom RAISERROR message indicated (see example)

Example:
--given the following CQL used within a saved query:

IF (ISNULL(@Int,0) = 0)
BEGIN
RAISERROR('Invalid parameter: @Int cannot be NULL or zero',18,0)
RETURN
END
IF (ISNULL(@String,'') = '' OR ISNULL(@String,'') = 'X')
BEGIN
RAISERROR('Invalid parameter: @String cannot be NULL or empty or X',18,0)
RETURN
END

--note "X-Cinchy-Error" in the sample response:

< HTTP/1.1 400 Bad Request
< Cache-Control: private
< Content-Type: text/html; charset=utf-8
< Server: Microsoft-IIS/10.0
< X-AspNetMvc-Version: 5.2
< Access-Control-Allow-Origin: *
< X-Cinchy-Error: Invalid parameter: @Int cannot be NULL or zero
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< Date: Wed, 1 Aug 2020 17:43:04 GMT
< Content-Length: 4517

401: The saved query API endpoint will return a 401 error code for invalid credentials. This includes missing, expired, or incorrect credentials.

< HTTP/1.1 401 Unauthorized
< Cache-Control: private
< Content-Type: text/html; charset=utf-8
< Server: Microsoft-IIS/10.0
< X-AspNetMvc-Version: 5.2
< X-AspNet-Version: 4.0.30319

Example

In this example, we want to use an API to get the results from the below simple query:

SELECT [Title]
FROM [Product].[Product Content Backlog]
WHERE [Deleted] IS NULL

Passing in the REST API URL and a Personal Access Token returns a successful 200 response.

Saved Query API Example

Anonymous access

The following instructions detail how to allow anonymous access to your saved query API endpoint.

note

Anonymous access supports GET actions only.

  1. Navigate to the table your query will be referencing. In this example, it's the Accessibility Assessments table (Image 1).
  2. Navigate to Data Controls > Entitlements.
  3. On a new row, add in the Anonymous user and ensure that either "View All Columns" (to expose all the data) or "View Selected Columns" (to select individual columns) is checked off (Image 3).

Image 3: The Entitlements Table

  1. Design your query (Image 4). For more information on creating new saved queries, click here.

Image 4: Design your Query

  1. Once you have written your query, navigate to Design Query > Info, on the left navigation bar.

  2. Change your API Result Format to JSON (Image 5).

Image 5: Change your API Result Format to JSON

  1. Navigate to Design Controls from the left navigation bar.

  2. To ensure that anonymous users have the correct permission needed to execute the query that generates the API response, add the "Anonymous" user to the users permission group UI under "Who can execute this query?" (Image 6).

Image 6: Ensuring the anonymous user has access

  1. Navigate to "Execute Query" from the left navigation bar.

  2. Copy your REST API endpoint URL (Image 7).

Image 7: Copy your REST API endpoint URL

  1. To confirm that anonymous access has been successfully set up, paste the URL into an incognito/private browser (Image 8).

Image 8: Testing your API

Troubleshooting

  • 401 errors likely mean you haven't added the Anonymous user to the list of users (not "Groups") that can execute the query.
  • 400 errors likely mean that you haven't added the Anonymous user to the list of users that (not "Groups") that can view column data from the tables that your query uses.