> ## Documentation Index
> Fetch the complete documentation index at: https://docs.smartpng.com/llms.txt
> Use this file to discover all available pages before exploring further.

# POST /compress

> Upload an image, compress it immediately, and return the optimized file as base64.

## Overview

Use this endpoint when you want SmartPNG to compress a single image synchronously and return the optimized file in the same response.

## Notes

* The request must be `multipart/form-data`.
* `api_key` and `file` are required.
* One successful compression consumes one credit.
* The response includes `compressed_data` as base64 and the updated `credits_remaining` value.


## OpenAPI

````yaml /openapi.yaml POST /compress
openapi: 3.0.3
info:
  title: SmartPNG Integration API
  description: >
    SmartPNG Integration API for backend services, CMS plugins, and automation
    flows.


    This API provides synchronous image compression with credit-based billing.


    ## Authentication

    Provide the API key in the request payload:

    - `/compress`: multipart form field `api_key`

    - `/validate`: JSON body field `api_key`


    ## Billing

    - Each successful compression costs `1` credit

    - Every account receives `500` free credits per month

    - Additional credits can be purchased from the SmartPNG dashboard
  version: 1.0.0
  contact:
    name: SmartPNG Support
    email: contact@smartpng.com
    url: https://smartpng.com
servers:
  - url: https://integration.smartpng.com/api
    description: Production
security: []
tags:
  - name: Compression
    description: Image compression operations
  - name: Validation
    description: API key validation and credit checks
  - name: Health
    description: Service status
paths:
  /compress:
    post:
      tags:
        - Compression
      summary: Compress an image
      description: >
        Upload a single image and receive the compressed result immediately in
        the response.


        Supported MIME types:

        - `image/png`

        - `image/jpeg`

        - `image/webp`

        - `image/avif`
      operationId: compressImage
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required:
                - api_key
                - file
              properties:
                api_key:
                  type: string
                  description: SmartPNG API key
                  example: sp_live_abc123def456ghi789
                file:
                  type: string
                  format: binary
                  description: Image file to compress
            encoding:
              file:
                contentType: image/png, image/jpeg, image/webp, image/avif
      responses:
        '200':
          description: Compression successful
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CompressionResponse'
              examples:
                success:
                  summary: Successful compression
                  value:
                    success: true
                    job_id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
                    status: completed
                    message: Image compressed successfully
                    compressed_data: >-
                      iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==
                    original_size: 125678
                    compressed_size: 78456
                    compression_ratio: 37.56
                    credits_remaining: 499
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                invalidContentType:
                  summary: Invalid content type
                  value:
                    error: Content-Type must be multipart/form-data
                noFile:
                  summary: File missing
                  value:
                    error: No file uploaded
                missingFields:
                  summary: Required fields missing
                  value:
                    error: 'Missing required fields: api_key, file'
                invalidFormat:
                  summary: Unsupported format
                  value:
                    error: Invalid file format
        '401':
          description: API key is invalid
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                invalidApiKey:
                  summary: Invalid API key
                  value:
                    error: Invalid API key
        '413':
          description: File exceeds the maximum size
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                fileTooLarge:
                  summary: Payload too large
                  value:
                    error: >-
                      Image size (10485860 bytes) exceeds maximum limit
                      (10485760 bytes)
        '429':
          description: No credits remaining
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InsufficientCreditsResponse'
              examples:
                noCredits:
                  summary: Insufficient credits
                  value:
                    error: >-
                      Insufficient credits. Please purchase more credits to
                      continue compression.
                    credits_remaining: 0
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                internalError:
                  summary: Internal server error
                  value:
                    error: Internal server error
components:
  schemas:
    CompressionResponse:
      type: object
      required:
        - success
        - job_id
        - status
        - message
        - compressed_data
        - original_size
        - compressed_size
        - compression_ratio
        - credits_remaining
      properties:
        success:
          type: boolean
          example: true
        job_id:
          type: string
          format: uuid
          example: a1b2c3d4-e5f6-7890-abcd-ef1234567890
        status:
          type: string
          enum:
            - completed
          example: completed
        message:
          type: string
          example: Image compressed successfully
        compressed_data:
          type: string
          format: byte
          description: Base64-encoded compressed image
          example: >-
            iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==
        original_size:
          type: integer
          example: 125678
        compressed_size:
          type: integer
          example: 78456
        compression_ratio:
          type: number
          format: float
          example: 37.56
        credits_remaining:
          type: integer
          example: 499
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          example: Internal server error
    InsufficientCreditsResponse:
      type: object
      required:
        - error
        - credits_remaining
      properties:
        error:
          type: string
          example: >-
            Insufficient credits. Please purchase more credits to continue
            compression.
        credits_remaining:
          type: integer
          example: 0

````