Go

Integrate the Flimpa API into your Go applications.

Installation

This example uses native Go packages to interact with the API. No external dependencies are required.

Initialization

Set up your API key and base URL.

go
const (
	APIKey  = "your_api_key"
	BaseURL = "https://api.flimpa.com/api/v1"
)

Authentication

Authenticate your requests using the X-API-Key header for task creation, and the X-Task-Access-Token header for status and download requests.

Usage Example

This example demonstrates how to create a task, poll for its status, and download the result. Operations supported are compress-pdf and image-to-webp.

go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"os"
	"time"
)

const (
	APIKey  = "your_api_key"
	BaseURL = "https://api.flimpa.com/api/v1"
)

type TaskResponse struct {
	TaskId      string `json:"taskId"`
	AccessToken string `json:"accessToken"`
	Status      string `json:"status"`
	PollUrl     string `json:"pollUrl"`
	Error       string `json:"error,omitempty"`
	Code        string `json:"code,omitempty"`
}

func main() {
	// 1. Create Task (Upload)
	file, err := os.Open("input.pdf")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)
	
	writer.WriteField("operation", "compress-pdf")
	part, _ := writer.CreateFormFile("file", "input.pdf")
	io.Copy(part, file)
	writer.Close()

	req, _ := http.NewRequest("POST", BaseURL+"/tasks", body)
	req.Header.Set("X-API-Key", APIKey)
	req.Header.Set("Content-Type", writer.FormDataContentType())

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil || resp.StatusCode >= 400 {
		var errResp map[string]interface{}
		json.NewDecoder(resp.Body).Decode(&errResp)
		panic(fmt.Sprintf("Failed to create task: %v", errResp["error"]))
	}

	var task TaskResponse
	json.NewDecoder(resp.Body).Decode(&task)
	resp.Body.Close()

	// 2. Poll Status
	delay := 1 * time.Second
	for {
		req, _ := http.NewRequest("GET", BaseURL+"/tasks/"+task.TaskId, nil)
		req.Header.Set("X-Task-Access-Token", task.AccessToken)
		
		resp, _ := client.Do(req)
		var status TaskResponse
		json.NewDecoder(resp.Body).Decode(&status)
		resp.Body.Close()

		if status.Status == "COMPLETED" {
			break
		}
		if status.Status == "FAILED" {
			panic(fmt.Sprintf("Task failed: %s (%s)", status.Error, status.Code))
		}

		time.Sleep(delay)
		if delay < 10*time.Second {
			delay *= 2
		}
	}

	// 3. Download Result
	req, _ = http.NewRequest("GET", BaseURL+"/tasks/"+task.TaskId+"/download", nil)
	req.Header.Set("X-Task-Access-Token", task.AccessToken)
	
	resp, _ = client.Do(req)
	defer resp.Body.Close()
	
	if resp.StatusCode >= 400 {
		panic("Failed to download result")
	}

	outFile, _ := os.Create("output.pdf")
	defer outFile.Close()
	io.Copy(outFile, resp.Body)

	fmt.Println("Success!")
}

Error Handling

Always check the response status and handle potential errors. The API returns JSON with error and code fields when a request fails.

Best Practices

  • Implement exponential backoff for polling (as shown in the example).
  • Files are limited to 20MB.
  • Tasks have a TTL of 1 hour. Download your results promptly.
  • Do not expose your API key in client-side code.

Webhooks

Webhooks for task completion are Coming Soon. For now, please use polling.