Example: Making Fast API Requests
While we strive to make the API as fast as possible, many performance factors are dependant on the API client itself. Below are some tips for improving the overall performance of your application as it interacts with the API.
- Authentication
- Compress large responses
- Use the _select parameter
- _expand wisely
- Be scrutinous when choosing a JSON parser
Step 1: Authentication
This example expects that you have already obtained an OAuth 2 authorization token to access a user's data. Read more on our OpendID Connect Authentication and OAuth 2 Authorization.
Step 2: Compress large responses
Especially for responses with large payloads, requesting the response be gzipped will save a significant amount of transmission time. Remember that if your response is compressed, you will need to uncompress it prior to processing.
In the example below, we are requesting that the response be gzipped by the following header:
Accept-Encoding: gzip, deflate
$ curl "https://sparkapi.com/v1/standardfields" -H "Authorization: OAuth MY_OAUTH2_ACCESS_TOKEN" -H "Accept-Encoding: gzip, deflate"
---- THE RESPONSE WILL BE COMPRESSED ----
Step 3: Use the _select parameter
The Attribute Selection Parameter greatly reduces the size of data payloads by returning only the fields you plan to use. Typically a few unselected fields will also be present, such as the Id
for each record and any relevant Compliance Rules.
$ curl "https://sparkapi.com/v1/listings?_select=ListingId,City,StateOrProvince,PostalCode" -H "Authorization: OAuth MY_OAUTH2_ACCESS_TOKEN"
{
"D": {
"Success": true,
"Results": [
{
"Id":"20100000000000000000000000",
"StandardFields": {
"ListingId":"1000000",
"City":"Fargo",
"PostalCode":"58102",
"MlsId":"20090000000000000000000000",
"StateOrProvince":"ND"
},
"DisplayCompliance": {
"View":"Detail",
"IDXLogoSmall": {
"LogoUri":"IDX",
"Type":"Text"
},
"IDXLogo": {
"LogoUri":null,
"Type":"Text"
}
}
}
]
}
}
Step 4: _expand wisely
Expansions are not included in the default response for good reason: they tend to slow things down, either by requiring more work of the API or by significantly increasing the response size. Thus, only request an expansion when you need it, and look for alternatives to larger expansions when available (e.g. consider using the PrimaryPhoto
expansion over the Photos
expansion in the Listings service.
The response below is abridged for clarity.
$ curl "https://sparkapi.com/v1/listings/20100000000000000000000000?_expand=PrimaryPhoto" -H "Authorization: OAuth MY_OAUTH2_ACCESS_TOKEN"
{
"D": {
"Success": true,
"Results": [
{
"ResourceUri": "/vX/listings/20100000000000000000000000",
"Id": "20100000000000000000000000",
"StandardFields": {
"ListingId": "1000000",
"PropertyType": "A",
"PropertySubType": "SF",
"Photos": [
{
"ResourceUri": "/vX/listings/20100000000000000000000000/photos/20080917142739989238000000",
"Id": "20080917142739989238000000",
"Name": "Listing Photo",
"Caption": "",
"UriThumb": "http://photos.sparkplatform.com/demomls/20080917142739989238000000-t.jpg",
"Uri300": "http://photos.sparkplatform.com/demomls/20080917142739989238000000.jpg",
"Uri640": "http://resize.sparkplatform.com/demomls/640x480/true/20080917142739989238000000-o.jpg",
"Uri800": "http://resize.sparkplatform.com/demomls/800x600/true/20080917142739989238000000-o.jpg",
"Uri1024": "http://resize.sparkplatform.com/demomls/1024x768/true/20080917142739989238000000-o.jpg",
"Uri1280": "http://resize.sparkplatform.com/demomls/1280x1024/true/20080917142739989238000000-o.jpg",
"UriLarge": "http://photos.sparkplatform.com/demomls/20080917142739989238000000-o.jpg",
"Primary": true
}
]
}
}
]
}
}
Step 5: Be scrutinous when choosing a JSON parser
JSON parsing takes time, and all parsers are not equal. Test out your parser of choice on a large set of JSON data even before you run into speed issues to be sure your parser selection is optimal.