PunyPNG API

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:


http://www.punypng.com/api/optimize?img=http://www.website.com/img/logo.png&key=YOUR_API_KEY

  • 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:


{"original_size":14641,"optimized_size":14041,"optimized_url":"http://www.punypng.com/processor/download_image/01ba280350afff7a03a3b5db78ec9a43515b012b?filename=logo.png","group_id":null,"savings_percent":5,"savings_bytes":600}
	

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).


http://www.punypng.com/api/optimize?img=http://www.website.com/img/logo.png&key=YOUR_API_KEY&group_id=eb1955a35625ea97e107132cbd49b65840d35sds
	

  • 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):


http://www.punypng.com/api/download_zip?group_id=eb1955a35625ea97e107132cbd49b65840d35sds
	

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):


{"indexed_savings_bytes":6571,"indexed_size":8070,"original_size":14641,"optimized_size":14041,"optimized_url":"http://www.punypng.com/processor/download_image/7ebfb05a51be4fc934ca949ca6b5199cdc78937e?filename=logo.png","group_id":null,"indexed_url":"http://www.punypng.com/processor/download_image/7ebfb05a51be4fc934ca949ca6b5199cdc78937e?filename=logo.indexed.png","savings_percent":5,"indexed_savings_percent":45,"savings_bytes":600}
	

Examples:

Ruby:


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

url = 'http://www.punypng.com/api/optimize'
key = YOUR_API_KEY
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