Examples
This section has not been updated to align with Crisisworks 5.
Basic Examples
The following examples show record creation and search using CURL.
Authentication
Read the section on Authentication for worked examples on obtaining and using authentication credentials.
Connecting using Linux Bash
In curl:
curl -X GET "https://api.cw.crisisworks.com/api/json/ping" \
-H "Authorization: eyABCDEFG..." \
-H "X-Site: sandbox"Connecting using Windows PowerShell
In Windows PowerShell:
Invoke-RestMethod `
-Method Get `
-Uri "https://api.cw.crisisworks.com/api/json/ping" `
-Headers @{
"Authorization" = "eyABCDEFG..."
"X-Site" = "sandbox"
}Record manipulation and searching examples
The following examples show record creation and search.
Record creation
The following example in Bash demonstrates how to create a register item record.
#!/bin/bash
# --- Configuration ---
HOST="api.cw.crisisworks.com"
SITE="mysite"
REGISTER_ID="recoveryInfrastructure"
TOKEN=$(cat ~/.crisisworks_token) # don't hardcode
# --- Create JSON payload ---
read -r -d '' PAYLOAD <<EOF
{
"title": "Test submission via API",
"requestor": "Scott Davey",
"registerId": "$REGISTER_ID",
"field1": "value1",
"field2": "value2"
}
EOF
# --- Submit POST request ---
RESPONSE=$(curl -s -X POST "https://${HOST}/api/json/item" \
-H "Accept: application/json" \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Bearer ${TOKEN}" \
-H "X-Site: ${SITE}" \
-d "${PAYLOAD}")Record search
The following example in Bash demonstrates how to search items. This uses curl and jq.
#!/bin/bash
# --- Configuration ---
HOST="api.cw.crisisworks.com"
SITE="mysite"
REGISTER_ID="recoveryInfrastructure"
TOKEN=$(cat ~/.crisisworks_token) # don't hardcode
# -- Search term --
REGISTER_ID="recoveryCase"
CQL="is:active event:22 dateUpdated:>now-30d"
CQL_URL_ENCODED=$(jq -rn --arg x "$CQL" '$x|@uri')
#CQL_URL_ENCODED will look like this: is%3Aactive+event%3A22+dateUpdated%3A%3Enow-30d
# --- Submit GET request to search ---
RESPONSE=$(curl -s -X GET "https://${HOST}/api/json/item?registerId=${REGISTER_ID}&cql=${CQL_URL_ENCODED}'
-H "Accept: application/json" \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Bearer ${TOKEN}" \
-H "X-Site: ${SITE}")
# --- Parse the response with jq, etc ---
Record updates
The following example shows how to update a record
#!/bin/bash
# --- Configuration ---
HOST="api.cw.crisisworks.com"
SITE="mysite"
REGISTER_ID="recoveryInfrastructure"
TOKEN=$(cat ~/.crisisworks_token) # don't hardcode
# --- This is the ID of the record being changed ---
ID=123
# --- Create JSON payload with the fields being changed ---
read -r -d '' PAYLOAD <<EOF
{
"id": "$ID",
"field1": "value1",
"field2": "value2"
}
EOF
# --- Submit PUT request to update an existing item ---
RESPONSE=$(curl -s -X PUT "https://${HOST}/api/json/item/id/${ID}" \
-H "Accept: application/json" \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Bearer ${TOKEN}" \
-H "X-Site: ${SITE}" \
-d "${PAYLOAD}")Further Examples
The following examples provide quick integrations for popular requirements.
Displaying all Fire Prevention Notices on a map using GeoJSON
This example displays a set of icons for each fire prevention notice record in Crisisworks on an OpenLayers map.
Steps
Obtain your authentication token
Prepare and test a URL to fetch the data
Add the URL into OpenLayers
curl -X GET "https://${HOST}/api/geojson/item?event=1014®isterId=fpn&active=1" \
-H "Authorization: Bearer ${TOKEN} \
-H "X-Site: ${TOKEN}" > map-data.geojsonThe resulting file map-data.geojson would look like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"coordinates": [
[
[
144.32965753,
-37.74266446
],
[
144.32848934,
-37.73833017
],
[
144.34002576,
-37.73953299
],
[
144.34046432,
-37.73957869
],
[
144.34008872,
-37.74173357
],
[
144.3397433,
-37.74371525
],
[
144.32965753,
-37.74266446
]
]
]
},
{
"type": "Point",
"coordinates": [
144.32965753,
-37.74266446
]
}
]
},
"properties": {
"iconFile": "ico_fire_yellow.png",
"iconShadow": "sq_shadow_full.png",
"iconSize": "[messa32,40]",
"iconAnchor": "[16,34]",
"style": "yellow",
"status": "yellow",
"id": "14968",
"title": "PARCEL 45530943",
"url": "https:\/\/demo4.crisisworks.com\/register\/item\/view\/id\/14968",
"info": [
"Status: Hazard Exists",
"Updated: 2016-06-20 14:06:27"
]
},
"_hasLineStrings": false
}
]
}Step 3
Add the URL above into your system as a GeoJSON layer. If developing on OpenLayers, this tutorial explains how to add a GeoJSON layer.
Importing asset contacts via the API
Example of importing asset contacts from a CSV file.
Note the data used in this example is embedded CSV within the JSON data. If you have a CSV file, look at the next example.
###
# Uses the asset contact import
#
POST /api/json/import
{
"mode": "schedule",
"dataType": "application/csv",
"data": "Asset Code,Code Field,Title,First Name,Last Name,Company,Full Name,Address 1,Address 2,Address 3,Address 4,Email,Mobile,Phone,Registers,Notes,External ID,Relationship to Property\n43626,propnum,Mrs,Joy,Brown,ACMI Corp,,3 Bourke Street,Melbourne,VIC,3000,[email protected],0123 45667,,fpn,This is the CEO,,Owner\n43627,propnum,Mr,Fred,Smith,Datalink,,2 Foo Street,Fooville,,,[email protected],,,,,,\n43628,propnum,Mrs,Colleen,Davey,Colgate,,3 Elizabeth St,Melbourne,VIC,3000,[email protected],,,,,,\n",
"encoding": "plain",
"selectedImportType": "Assets",
"importType":"Assets",
"importRule": "contact",
"rollbackOnError": false,
"message": "Test Asset Contacts import",
"regionId": "DEV1"
}
Uploading an import file
The following example shows how you may upload a CSV file to the import API, using BASH.
#!/bin/bash
# Auth credentials
HOST="api.cw.crisisworks.com"
SITE="mysite"
TOKEN="your token"
# The file to send
IMPORT_FILE="myfile.csv"
IMPORT_TYPE="Assets"
IMPORT_RULE="contact"
IMPORT_REGION="your site region"
IMPORT_MESSAGE="Import of asset contacts"
AUTH="Authorization: Bearer ${TOKEN}"
JSON="{\"mode\": \"schedule\",\"data\": \"$(base64 --input=${IMPORT_FILE})\",\"dataType\": \"application/csv\",\"encoding\": \"base64\",\"importType\": \"${IMPORT_TYPE}\",\"importRule\": \"${IMPORT_RULE}\",\"rollbackOnError\": false,\"message\": \"${IMPORT_MESSAGE}\",\"regionId\": \"${IMPORT_REGION}\"}"
curl -X POST \
-d ${JSON} \
-H 'Accept: application/json' \
-H ${AUTH} \
-H 'Content-Type: application/json\;charset=UTF-8' \
-H 'X-Site: ${SITE}' \
"https://${HOST}/api/json/import" Here is the equivalent in Windows Powershell.
# Auth credentials
$HOST = "api.cw.crisisworks.com"
$SITE = "mysite"
$TOKEN = "your token"
# File and import metadata
$IMPORT_FILE = "myfile.csv"
$IMPORT_TYPE = "Assets"
$IMPORT_RULE = "contact"
$IMPORT_REGION = "your site region"
$IMPORT_MESSAGE = "Import of asset contacts"
# Encode file to Base64
$Base64Data = [Convert]::ToBase64String([IO.File]::ReadAllBytes($IMPORT_FILE))
# Build the JSON payload
$Payload = @{
mode = "schedule"
data = $Base64Data
dataType = "application/csv"
encoding = "base64"
importType = $IMPORT_TYPE
importRule = $IMPORT_RULE
rollbackOnError = $false
message = $IMPORT_MESSAGE
regionId = $IMPORT_REGION
} | ConvertTo-Json -Depth 10
# Set headers
$Headers = @{
"Accept" = "application/json"
"Content-Type" = "application/json;charset=UTF-8"
"Authorization" = "Bearer $TOKEN"
"X-Site" = "$SITE"
}
# Send the POST request
Invoke-RestMethod -Uri "https://$HOST/api/json/import" `
-Method Post `
-Headers $Headers `
-Body $PayloadAdding images to Infrastructure Assessments
Attaching images to Register Items in Crisisworks happens in 2 steps.
Upload the image
First, the image needs to be be uploaded to Crisisworks. On successful upload, a file id is returned. Example Request
curl --location 'https://$HOST/api/json/file' \
--header 'X-Site: $YOUR_SITE' \
--header 'Authentication: Bearer $YOUR_TOKEN' \
--form 'file=@"$PATH_TO_FILE"' \
--form 'filename="test-api-upload.png"' \
--form 'uuid="$YOUR_DEVICE_UUID"' \Example Response:
{"code":200,"message":"OK","id":"289"}The file id 289 from the response is used to update the register item.
Update the item to attached the file The specific item then needs to be updated to include the file. In the case of an Infrastructure Item, we are adding to the "Damage Photos" - field name descriptionPhotos
curl --location --request PUT 'https://$HOST/api/json/item/id/220' \
--header 'X-Site: $YOUR_SITE' \
--header 'Authentication: Bearer $YOUR_TOKEN' \
--header 'Content-Type: application/json' \
--data '{
"uuid":"$YOUR_DEVICE_UUID",
"token":"$YOUR_TOKEN",
"type":"recoveryInfrastructure",
"itemSchemaName":"default",
"id": "220",
"infrastructureType": "facility",
"damageLevel": "6",
"description":"<p>qew</p>",
"priority":"2",
"eventId":"26",
"descriptionPhotos": [
{
"id": "289",
"name": "test-api-upload.png",
"type": "image/png",
"path": null
}
]
}'Please note, when updating an item, all the required fields also need to be included in the payload, so you will need to get this data before making any updates.
In the instance of Infrastructure Assessment, the required fields are:
Field Name
Description
id
The item ID of the record being updated.
type
Should be set to "recoveryInfrastructure".
itemSchemaName
Set to "default".
infrastructureType
Set to the existing value for this field.
damageLevel
Set to the existing value for this field.
description
Set to the existing value for this field.
priority
Set to the existing value for this field.
eventId
Set to the existing value for this field.
Using Windows Command Prompt
Users may receive the following message when attempting to register an API Token when using Windows Command Prompt.
{“code”:400, “message”:”Missing parameters”}Windows command prompt has no support for single quotes like the Unix-like shells. As a result, you will need to replace the single quotes with double quotes AND escape them wherever necessary using back slashes.
Example
curl -d "{\"uuid\": \"$YOUR_DEVICE_UUID\",\"platform\": \"Windows\",\"version\": \"1\",\"name\": \"$YOUR_NAME\",\"username\": \$YOUR_USERNAME\,\"password\": \"$YOUR_PASSWORD\"}" -H "Content-Type: application/json"Last updated
Was this helpful?
