# Best practices

## Use GZIP compression

Compression reduces the amount of data being transferred and improves data request speed. Add HTTP headers to enable GZIP compression.

<CodeTabs syncKey="language">

```ts title="TypeScript"
const url = "https://dataservice.accuweather.com/locations/v1/search?q=san";

const response = await fetch(url, {
  headers: {
    "Authorization": "Bearer YOUR_API_KEY",
    "Accept-Encoding": "gzip",
  },
});

const data: Record<string, unknown>[] = await response.json();
```

```python title="Python"
import requests

url = "https://dataservice.accuweather.com/locations/v1/search"
params = {"q": "san"}
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Accept-Encoding": "gzip",
}

response = requests.get(url, params=params, headers=headers)
data = response.json()
```

```csharp title="C#"
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_API_KEY");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");

var url = "https://dataservice.accuweather.com/locations/v1/search?q=san";
var response = await client.GetAsync(url);
var data = await response.Content.ReadAsStringAsync();
```

```java title="Java"
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://dataservice.accuweather.com/locations/v1/search?q=san"))
    .header("Authorization", "Bearer YOUR_API_KEY")
    .header("Accept-Encoding", "gzip")
    .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String data = response.body();
```

```bash title="bash"
curl -H "Authorization: Bearer YOUR_API_KEY" \
     -H "Accept-Encoding: gzip,deflate" \
     "https://dataservice.accuweather.com/locations/v1/search?q=san"
```

</CodeTabs>

- Without compression: 17,695 bytes
- With compression: 2,958 bytes
- Size reduction of 83%

## Randomize refresh rates

Randomize individual device refresh rates so that devices refresh at different clock times. Requesting updates on all devices at consistent times will overload the system.

## Use the _expires_ header

Refresh information from the AccuWeather APIs for your device based upon the cache expires time in the response headers. In the example below, refresh on Thursday, August 30th, 2012 at 14:56:34 GMT.

```
Response Headers
    Cache-Control: public
    Content-Encoding: gzip
    Content-Type: application/json; charset=utf-8
    Date: Wed, 29 Aug 2012 14:55:33 GMT
    Expires: Thu, 30 Aug 2012 14:56:34 GMT
    Server: Microsoft-IIS/7.5
    Server: Microsoft-IIS/7.0
    Transfer-Encoding: chunked
    Vary: Accept-Encoding
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
```

## Timezone offset changes for daylight saving time

If you intend to use the GMTOffset from the Location API response to calculate times local to the location, you MUST be careful to observe the NextOffsetChange property. The offset will change on the date and time specified. Using the _expires_ header as described above will ensure that you have the most current GMTOffset for the location.

## Use HTTPS Only

HTTPS encrypts traffic between the client and AccuWeather servers, preventing data leaks, interception, and tampering.

Always call our APIs using the documented HTTPS endpoints.

## Redirects

We may temporarily support HTTP by automatically upgrading the connection to HTTPS for backward compatibility.

This behavior is not guaranteed long-term and should not be relied upon.

Please update integrations to use HTTPS endpoints exclusively.
