Go · Code recipe
Get the rendered HTML of a JavaScript page in Go
Fetch the HTML a browser ends up with, JavaScript and all, in Go 1.21+ with net/http from the standard library. Every program on this page runs as it stands — each one was run against a stub of the API before it was published.
By Roger Campos · Last updated: September 2026
TL;DR
To get the HTML of a JavaScript-rendered page in Go, POST its URL to https://urlpipe.dev/html with sync set to true. The body is the DOM after the page's scripts ran in real Chrome, serialized as HTML — what a visitor's browser holds, not what the server first sent — read with io.ReadAll, for 1 credit.
Free plan, no credit card. 1,000 credits a month.
One POST to /html loads the page in real Chrome, lets its JavaScript run, and serializes the DOM it ended up with. For a single-page app that is the difference between an empty <div id="root"> and the content; the rendered vs raw HTML guide shows how far apart the two get.
The body is the HTML as text, read with io.ReadAll. The program saves it and reports its size in bytes — compare that with curl -s https://example.com | wc -c and you see how much of the page only exists after the scripts ran.
Setup
Before you start
Nothing to fetch: every import is standard library, so each program runs with go run as a single file. Put your API key in the environment.
go version # go1.21 or later
export URLPIPE_API_KEY="your_api_key"
The request
Save the rendered HTML and report its size
"sync": true keeps the request open until the result is ready. http.Client has no timeout unless you set one, and res.Body is a stream: io.ReadAll turns it into bytes, and defer res.Body.Close() hands the connection back. len(body) on a byte slice is the size in bytes, which is the number you want.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
func main() {
payload, err := json.Marshal(map[string]any{
"url": "https://example.com",
"sync": true,
})
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(http.MethodPost, "https://urlpipe.dev/html", bytes.NewReader(payload))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("URLPIPE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
// A sync call can take up to 60 s; the zero-value client would wait forever.
client := &http.Client{Timeout: 90 * time.Second}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
// The body is the result or the error, so read all of it first.
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
if res.StatusCode != http.StatusOK {
log.Fatalf("URLpipe answered %d: %s", res.StatusCode, body)
}
if err := os.WriteFile("page.html", body, 0o644); err != nil {
log.Fatal(err)
}
fmt.Printf("Saved page.html (%d bytes)\n", len(body))
}
Run it: go run main.go
Async
The async variant: a token, a webhook and a poll
Leave out sync and the answer is a token, straight away. Decode only the field you need into an anonymous struct; switch on the status covers the four answers GET /result/:token can give.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
const api = "https://urlpipe.dev"
var client = &http.Client{Timeout: 30 * time.Second}
// send makes one request to the API and returns the status and the body.
func send(method, path string, payload any) (int, []byte) {
var reader io.Reader
if payload != nil {
data, err := json.Marshal(payload)
if err != nil {
log.Fatal(err)
}
reader = bytes.NewReader(data)
}
req, err := http.NewRequest(method, api+path, reader)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("URLPIPE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
return res.StatusCode, body
}
// poll collects a result by token. The result is POSTed to report_to when it is
// ready; polling is the other way to collect it, and a backup for the webhook.
func poll(token string) []byte {
for attempt := 0; attempt < 60; attempt++ {
status, body := send(http.MethodGet, "/result/"+token, nil)
switch status {
case http.StatusOK:
return body
case http.StatusAccepted: // still processing
time.Sleep(2 * time.Second)
case http.StatusUnprocessableEntity:
log.Fatalf("The analysis failed: %s", body)
case http.StatusGone:
log.Fatal("The result is past the 30-day window; send the request again.")
default:
log.Fatalf("URLpipe answered %d: %s", status, body)
}
}
log.Fatal("Still processing after two minutes; try the token again later.")
return nil
}
func main() {
// No "sync": the request is accepted at once and the work carries on without you.
status, body := send(http.MethodPost, "/html", map[string]any{
"url": "https://example.com",
"report_to": "https://your-app.com/webhooks/urlpipe",
"labels": map[string]any{"customer": "acme"},
})
if status != http.StatusOK {
log.Fatalf("URLpipe answered %d: %s", status, body)
}
var accepted struct {
Token string `json:"token"`
}
if err := json.Unmarshal(body, &accepted); err != nil {
log.Fatal(err)
}
fmt.Println("Accepted", accepted.Token)
body = poll(accepted.Token)
if err := os.WriteFile("page.html", body, 0o644); err != nil {
log.Fatal(err)
}
fmt.Printf("Saved page.html (%d bytes)\n", len(body))
}
Run it: go run main.go
Errors
Handle errors and retries
Errors are values: urlpipe() returns one for every refusal it will not retry, and main decides what to do with it. A 401 answers in plain text, so it is handled before the JSON decode; max is a builtin from Go 1.21.
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
)
var client = &http.Client{Timeout: 90 * time.Second}
// urlpipe POSTs a sync request and returns the result body. It retries the two
// 429s that clear by themselves and returns an error for everything else.
func urlpipe(path string, payload map[string]any) ([]byte, error) {
payload["sync"] = true
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
const attempts = 5
for attempt := 0; attempt < attempts; attempt++ {
req, err := http.NewRequest(http.MethodPost, "https://urlpipe.dev"+path, bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("URLPIPE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, err
}
switch res.StatusCode {
case http.StatusOK:
return body, nil
case http.StatusUnauthorized:
return nil, errors.New("401: the API key is missing or wrong, check URLPIPE_API_KEY")
}
var e struct {
Code string `json:"error"`
Message string `json:"message"`
Token string `json:"token"`
}
_ = json.Unmarshal(body, &e) // a body that is not JSON leaves e empty
detail := e.Code
if e.Message != "" {
detail = e.Code + ": " + e.Message
}
switch {
case res.StatusCode == http.StatusTooManyRequests && e.Code == "rate_limited":
// Sending too fast: Retry-After says how long the window has left.
seconds, _ := strconv.Atoi(res.Header.Get("Retry-After"))
time.Sleep(time.Duration(max(seconds, 1)) * time.Second)
case res.StatusCode == http.StatusTooManyRequests && e.Code == "concurrency_limit":
// Every parallel slot on your plan is busy with your own requests.
time.Sleep(time.Duration(1<<attempt) * time.Second)
case res.StatusCode == http.StatusGatewayTimeout:
// Still running on our side; the token collects it from GET /result/:token.
return nil, fmt.Errorf("504 processing_timeout: collect it later with token %s", e.Token)
default:
// 403 email_unverified, 422 (a bad parameter, or a page that would not load),
// 429 quota_exceeded: sending the same request again gets the same answer.
return nil, fmt.Errorf("%d: %s", res.StatusCode, detail)
}
}
return nil, fmt.Errorf("429: still refused after %d attempts", attempts)
}
func main() {
body, err := urlpipe("/html", map[string]any{
"url": "https://example.com",
})
if err != nil {
log.Fatalf("URLpipe: %v", err)
}
if err := os.WriteFile("page.html", body, 0o644); err != nil {
log.Fatal(err)
}
fmt.Printf("Saved page.html (%d bytes)\n", len(body))
}
Run it: go run main.go
Details
What to know about /html
- The HTML is serialized from the live DOM, so it is well-formed but not byte-identical to any file on the server.
- Pages over 10 MB of HTML are refused with
The page is too big to be processed. page_options.wait_for_selectorwaits for an element that loads late;delaywaits a fixed time on top.- It is the whole document, scripts and styles included, not a cleaned-up version of it. For clean text, use Markdown instead.
FAQ
Frequently asked questions
How is this different from fetching the URL myself?
Can I wait for content that loads late?
Does URLpipe follow robots.txt?
Do I need an SDK to call URLpipe from Go?
Get a key and run it.
Free plan, no card. Paste your key into URLPIPE_API_KEY and every program on this page runs as it is.