# GIF Compressor

Compress GIF images and animations to reduce file size. Supports both static and animated GIFs.

<p><a href="/image/gif" style="color:#1472ef;font-weight:600;font-size:0.875rem;text-decoration:none">Try GIF compression in the browser &rarr;</a></p>

Compress.FAST uses this same API — what you see in the browser is what you get in code.

## Quick reference

| | |
|---|---|
| **Job type** | `image.gif-compress` |
| **Category** | images |
| **Endpoint** | `POST /compress/gif` |
| **Input formats** | `.gif` |
| **Output format** | same as input |
| **Costing** | 1 credit per 5 MB |
| **Max file size** | 1 GB |
| **Options** | resize |

## Example

<!-- code-tabs:start default=curl -->
### cURL
```bash
# 1) submit
JOB_ID=$(curl -sS -X POST "https://api.tools.fast/compress" \
  -H "X-Fast-Api-Key: $API_KEY" \
  -F "file=@animation.gif" | jq -r '.id')

# 2) poll until complete
while true; do
  STATUS=$(curl -sS "https://api.tools.fast/compress/job/${JOB_ID}" \
    -H "X-Fast-Api-Key: $API_KEY" | jq -r '.status')
  [ "${STATUS}" = "Succeeded" ] && break
  [ "${STATUS}" = "Failed" ] || [ "${STATUS}" = "Canceled" ] && exit 1
  sleep 1
done

# 3) download
curl -sS "https://api.tools.fast/compress/job/${JOB_ID}/download" \
  -H "X-Fast-Api-Key: $API_KEY" \
  -o "animation-compressed.gif"
```

### C#
```csharp
using var http = new HttpClient();
http.DefaultRequestHeaders.Add("X-Fast-Api-Key", "fast_prod_your_key_here");

using var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(File.ReadAllBytes("animation.gif")), "file", "animation.gif");

var submit = await http.PostAsync("https://api.tools.fast/compress", form);
var job = await submit.Content.ReadFromJsonAsync<JsonElement>();
var jobId = job.GetProperty("id").GetString();

string status;
do
{
    await Task.Delay(1000);
    var poll = await http.GetFromJsonAsync<JsonElement>(
        $"https://api.tools.fast/compress/job/{jobId}");
    status = poll.GetProperty("status").GetString()!;
} while (status is "Queued" or "Running");

var output = await http.GetByteArrayAsync(
    $"https://api.tools.fast/compress/job/{jobId}/download");
File.WriteAllBytes("animation-compressed.gif", output);
```

### PowerShell
```powershell
$headers = @{ "X-Fast-Api-Key" = "fast_prod_your_key_here" }

# 1) submit
$job = Invoke-RestMethod -Method Post "https://api.tools.fast/compress" `
  -Headers $headers `
  -Form @{ file = Get-Item "animation.gif" }

# 2) poll until complete
do {
  $status = (Invoke-RestMethod "https://api.tools.fast/compress/job/$($job.id)" `
    -Headers $headers).status
  if ($status -in "Failed", "Canceled") { throw "Job $($job.id) $status" }
  Start-Sleep -Seconds 1
} while ($status -ne "Succeeded")

# 3) download
Invoke-RestMethod "https://api.tools.fast/compress/job/$($job.id)/download" `
  -Headers $headers -OutFile "animation-compressed.gif"
```
<!-- code-tabs:end -->

## Options

The GIF compressor supports **resize** -- scale down images and animations while compressing. See [Options](options.md#resize) for the full resize reference.

<!-- code-tabs:start default=curl -->
### cURL
```bash
# compress + resize to 720p
curl -sS -X POST "https://api.tools.fast/compress" \
  -H "X-Fast-Api-Key: $API_KEY" \
  -F "file=@animation.gif" \
  -F 'options={"_schemaVersion":1,"resize":{"enabled":true,"preset":"720p","mode":"pixels","value":null}}'
```

### C#
```csharp
using var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(File.ReadAllBytes("animation.gif")), "file", "animation.gif");
form.Add(new StringContent(
    """{"_schemaVersion":1,"resize":{"enabled":true,"preset":"720p","mode":"pixels","value":null}}"""),
    "options");

var response = await http.PostAsync("https://api.tools.fast/compress", form);
```

### PowerShell
```powershell
Invoke-RestMethod -Method Post "https://api.tools.fast/compress" `
  -Headers @{ "X-Fast-Api-Key" = $Key } `
  -Form @{
    file    = Get-Item "animation.gif"
    options = '{"_schemaVersion":1,"resize":{"enabled":true,"preset":"720p","mode":"pixels","value":null}}'
  }
```
<!-- code-tabs:end -->

## Notes

- **Output format: `.gif`** -- same as input.
- Animation frames and timing are preserved during compression.
- The compressor optimizes the color palette and frame data to reduce file size.
- For static GIF images, consider converting to PNG or WebP for better compression ratios (use [Convert.FAST](https://convert.fast/api) for format conversion).

## Related

- [JPG](jpg.md)
- [PNG](png.md)
- [Universal](universal.md)
