Using the PunyPNG API

The PunyPNG API is a great way to utilize the compressssion powers of PunyPNG in any of your own applications. The API works just like the web version, allowing you to compress any of the supported image formats. PunyPNG API will intelligently compress your image using its toolbox of algorithms.

There are some restriction in using this API (see below).

Getting started with the API

In order to access the API, you will need to create an account and copy the API key under your Account & Settings.

The PunyPNG is a REST-style API that returns a JSON object as its response. You will need to de-serialize the JSON object to access the results.

To optimize a single image:

You can call the PunyPNG API using a GET or POST request. A GET request uses a URL with parameters:


  • key: Your 40-bit API key
  • img: URL where the image resides

Calling the API using a POST request is very similar, except it is called from either the server backend or in a client-side script. Here's an example using Ruby that calls the API using a POST request.

After the GET/POST request, PunyPNG will download that file, optimized it and return a JSON response containing the URL of the optimized image as well as the summary of the savings. With the optimize image URL, you can download the optimized image from our servers.

Example of the JSON response:


To optimize multiple images:

The PunyPNG API also allows you to optimize multiple images and download all the optimized images in batch (much faster). To be able to group multiple optimizations, you need to generate a random 40-bit group_id across all your API calls (this is different than your API key).


  • key: Your 40-bit API key
  • img: URL where the image resides
  • group_id: a random 40-bit key that you generate

Repeat this call for each image using the same group_id.

When you're ready to download the ZIP, just refer to the group_id (no API key required):


Lossy PNG Support:

PunyPNG Pro gives you the ability to optimize your 24-bit transparent PNGs with a enhanced 8-bit indexed palette, while preserving full transparency. If your account has been upgraded to PunyPNG Pro, the API automatically includes access to these indexed, lossy PNGs in the JSON response. They are labeled with the 'indexed' prefix.

Example of the JSON response with Lossy PNG support (PunyPNG Pro only):




require 'rubygems'
require 'rake'
require 'httpclient'

url = 'http://www.punypng.com/api/optimize'
file = 'test.png'

response = HTTPClient.post url, { 'img' => File.new(file), 'key' => key }

puts response.body.inspect

PunyPNG API Restrictions:

  • 50 optimized images per month
  • Each image has a maximum size of 500KB
  • All your files live on the server for only 5 minutes for increased security

For increased API quotas and lossy PNG support, upgrade to PunyPNG Pro

Need help?

If you have any questions or issues, you can ask it on our Support page