---
canonical: 'https://htmlcsstoimage.com/features/html-to-jpeg/golang'
title: 'Convert HTML to JPEG in GoLang | HTML/CSS to Image'
description: 'Convert HTML and CSS to JPEG in GoLang. Generate a hosted .jpeg image URL.'
---

# Generate JPEG images in GoLang without managing Chrome

Render HTML and CSS from GoLang without installing Chrome, running chromedp or Rod, or building a separate image-storage pipeline.

## Why use the API instead of native GoLang browser automation?

- Keep the deployment as a small Go service instead of adding Chrome and a browser-controller process to the runtime.
- Use managed browser versions, isolated render jobs, timing controls, and consistent viewport behavior across deployments.
- Receive a hosted .jpeg URL without building a separate pipeline for temporary files, uploads, storage, and delivery.

## Complete GoLang request

Use this request to render the example HTML and CSS as JPEG.

```go
package main

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

func main() {
    request := map[string]any{
        "html": `
                <div class="card">
                  <h1>Hello, world!</h1>
                  <p>Generated from HTML and CSS.</p>
                </div>
                `,
        "css": `
               .card {
                 width: 480px;
                 padding: 40px;
                 background: #f0fdf4;
                 color: #052e16;
                 font-family: sans-serif;
               }
               `,
        "format": "jpeg",
    }

    body, err := json.Marshal(request)
    if err != nil {
        panic(err)
    }
    httpRequest, err := http.NewRequest(http.MethodPost, "https://hcti.io/v1/image", bytes.NewReader(body))
    if err != nil {
        panic(err)
    }
    httpRequest.SetBasicAuth(os.Getenv("HCTI_API_ID"), os.Getenv("HCTI_API_KEY"))
    httpRequest.Header.Set("Content-Type", "application/json")

    client := &http.Client{Timeout: 30 * time.Second}
    response, err := client.Do(httpRequest)
    if err != nil {
        panic(err)
    }
    defer response.Body.Close()
    responseBody, err := io.ReadAll(response.Body)
    if err != nil {
        panic(err)
    }
    if response.StatusCode >= http.StatusBadRequest {
        panic(fmt.Sprintf("HCTI request failed (%d): %s", response.StatusCode, responseBody))
    }

    var result struct {
        URL string `json:"url"`
    }
    if err := json.Unmarshal(responseBody, &result); err != nil {
        panic(err)
    }
    fmt.Println(result.URL)
}

```

## When to use JPEG

JPEG is a practical choice for photographic and content-rich images where broad compatibility and smaller files matter.

- **Smaller photo-heavy images**: Lossy compression keeps files compact when the rendered design contains photography, gradients, or detailed backgrounds.
- **Broad compatibility**: JPEG works across browsers, email clients, social platforms, document tools, and long-running application workflows.
- **Easy to distribute**: Use a familiar image format for previews, downloads, attachments, and automated publishing.

## API request

Send an authenticated `POST` request to `https://hcti.io/v1/image`. Include `html`, optional `css`, `format`, and any viewport or browser options required by the render.

```json
{
  "html": "\u003Carticle class=\u0027card\u0027\u003E...\u003C/article\u003E",
  "css": ".card { display: grid; width: 1200px; }",
  "format": "jpeg",
  "viewport_width": 1200,
  "viewport_height": 630
}
```

## Request workflow

1. **Send HTML and CSS** — Submit the content with format set to jpeg and any viewport or browser settings.
2. **Chromium renders the design** — The API loads the markup, styles, fonts, and assets in a managed browser environment.
3. **Use the .jpeg URL** — Embed the hosted result, download the file, or save it in your own storage and application records.

## Authentication and output handling

- Authenticate from trusted server-side code. Do not expose an API key in browser JavaScript, generated HTML, logs, or agent-visible output.
- Treat the returned URL as the generated `jpeg` resource. Download it, embed it, or copy it to configured storage as required by the workflow.
- Use viewport, selector, timing, and browser options when the default capture does not match the required dimensions or page state.
- If credentials are unavailable, ask the user to configure them securely before attempting an API request.

## Use through MCP

An MCP-compatible agent can connect to `https://mcp.hcti.io` with OAuth and use the connected HTML/CSS to Image account instead of handling an API key directly.

Ask the MCP tool to render the supplied HTML and CSS as JPEG, or specify `format: jpeg` explicitly when constructing the request.

- [Read the MCP integration documentation](https://docs.htmlcsstoimage.com/integrations/mcp/)

## Related conversions

- [URL to JPEG](https://htmlcsstoimage.com/features/url-to-jpeg/golang.md): Capture a public webpage as JPEG instead of supplying HTML and CSS.
- [HTML to PNG](https://htmlcsstoimage.com/features/html-to-png/golang.md): PNG preserves sharp edges, text, and transparency, making it ideal for interface captures and designed graphics.
- [HTML to WebP](https://htmlcsstoimage.com/features/html-to-webp/golang.md): WebP combines modern compression with high visual quality, making it a strong default for images delivered on the web.
- [HTML to PDF](https://htmlcsstoimage.com/features/html-to-pdf.md): Generate paginated documents with page sizing, margins, backgrounds, and print styles.

## Reference links

- [API authentication and request documentation](https://docs.htmlcsstoimage.com/getting-started/using-the-api/)
- [MCP integration documentation](https://docs.htmlcsstoimage.com/integrations/mcp/)
- [Interactive HTML to JPEG page](https://htmlcsstoimage.com/features/html-to-jpeg/golang)
- [Plans and current limits](https://htmlcsstoimage.com/pricing.md)
