Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

## 1.2.0 (unreleased)

### Features

- Image API: `uploadImage` (file path) and `uploadImageBytes` (in-memory
image), with typed `uploadImageAs` / `uploadImageBytesAs`, upload an image
to `POST https://serpapi.com/image` as `multipart/form-data` and return
its `image_id` for engines such as Google Lens. The `api_key` travels in
the form body, never in the URL; constructor search defaults (`engine`,
`output`, ...) are not sent. The content type is detected from the image
signature (JPEG, PNG, WebP), falling back to the file extension.
Images over 500 KiB (`serpapi.image_max_size`) are rejected with
`error.ImageTooLarge` before being read or sent
- The out-of-box test (`zig build oobt`) also uploads an image
- `Client.lastError()` returns a `serpapi.ErrorInfo` with the details of the
last failure, mirroring serpapi-ruby's `SerpApiError`: `message` (as
`errorMessage()`), the HTTP `status`, the request `url` without the
Expand Down Expand Up @@ -32,6 +44,8 @@ These ship in a minor release by decision; pin `v1.1.0` to keep the old API.
the key from `client.params.items` must call `apiKey()` instead
- `Client.last_error` is now a `?serpapi.ErrorInfo` instead of `?[]u8`;
`errorMessage()` is unchanged
- `serpapi.Error` gains `ImageTooLarge`: code that switches exhaustively
over `serpapi.Error` needs a prong for it

## 1.1.0 (2026-09-23)

Expand Down
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Query a vast range of data at scale, including web search results, flight schedu
## Features
* `persistent` → Keep socket connection open to save on SSL handshake / reconnection (3-4x faster on repeated searches).
* `md` → Markdown output, token-efficient and ready to feed to LLMs and AI agents. [Search API — Markdown](#search-api--markdown)
* `uploadImage` → upload a local image, then search it with Google Lens. [Google Lens with File Upload](#google-lens-with-file-upload)
* zero dependency → only the Zig standard library (`std.http`, `std.json`), nothing else to fetch.
* cross platform → Linux, macOS, and Windows.
* extensive documentation → easy to follow.
Expand Down Expand Up @@ -225,7 +226,8 @@ var results = try client.searchAs(Answer, .{ .q = "coffee" });
std.debug.print("status: {s}\n", .{results.value.search_metadata.status});
```

Also available: `locationAs`, `searchArchiveAs`, and `accountAs`.
Also available: `locationAs`, `searchArchiveAs`, `accountAs`,
`uploadImageAs`, and `uploadImageBytesAs`.

### Search API — raw HTML

Expand Down Expand Up @@ -293,6 +295,34 @@ The api_key provided to the constructor is used; override it with

doc: [serpapi.com/account-api](https://serpapi.com/account-api)

### Google Lens with File Upload

Upload an image with the Image API, then use its image ID in a Google Lens
search. Uploaded image IDs expire after 10 minutes.

```zig
var upload = try client.uploadImage("./image.jpg", .{});
const image_id = upload.value.object.get("image_id").?.string;

var results = try client.search(.{ .engine = "google_lens", .image_id = image_id });
const visual_matches = results.value.object.get("visual_matches");
```

The image already in memory? Pass its bytes and a file name instead:
`client.uploadImageBytes(bytes, "image.jpg", .{})`. The content type is
detected from the image signature (jpg, jpeg, png, webp), falling back to
the file name's extension.

The image is sent as `multipart/form-data` in a `POST` to
`https://serpapi.com/image`, with the `api_key` in the form body rather
than the URL; override it per call with `.{ .api_key = "other key" }`.
Search defaults from the constructor (`engine`, `output`, ...) are not sent.
Images larger than 500 KiB (`serpapi.image_max_size`) fail with
`error.ImageTooLarge` before anything is read or sent, and file errors such
as `error.FileNotFound` are returned as-is.

doc: [serpapi.com/image-api](https://serpapi.com/image-api) · [Google Lens image upload](https://serpapi.com/google-lens-upload-an-image)

## Error handling

Methods return a Zig error union. When serpapi.com reports a failure — an
Expand All @@ -315,8 +345,9 @@ Other errors, all with details in `client.errorMessage()`:
message is the raw body, or the status name when the body is empty),
`error.JsonParseError` (response was not valid JSON, or did not fit the
requested type), `error.Timeout` (the request overran the client's `timeout`),
plus any network / TLS / allocation errors propagated from the standard
library. The four client errors are grouped in `serpapi.Error`.
`error.ImageTooLarge` (an upload over `serpapi.image_max_size`, never sent),
plus any network / TLS / allocation / file errors propagated from the
standard library. The client errors are grouped in `serpapi.Error`.

`client.lastError()` returns the details of the last failure, mirroring
serpapi-ruby's `SerpApiError`: the `message` (same as `errorMessage()`), the
Expand Down
12 changes: 11 additions & 1 deletion oobt/demo.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Out-of-box testing (OOBT) demo application.
//!
//! Runs a real Google search through serpapi.com and prints the top
//! organic results. Usage:
//! organic results, then uploads a tiny image with the Image API. Usage:
//!
//! SERPAPI_KEY=secret_api_key zig build oobt

Expand Down Expand Up @@ -35,5 +35,15 @@ pub fn main(init: std.process.Init) !void {
const link = item.object.get("link").?.string;
std.debug.print(" - {s}\n {s}\n", .{ title, link });
}

// a 1x1 transparent PNG; the image_id it returns feeds a google_lens search
const pixel = "\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89" ++
"\x00\x00\x00\x0aIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01\x0d\x0a\x2d\xb4\x00\x00\x00\x00IEND\xaeB\x60\x82";
var upload = client.uploadImageBytesAs(struct { image_id: []const u8 }, pixel, "pixel.png", .{}) catch |err| {
std.debug.print("upload failed: {t} ({s})\n", .{ err, client.errorMessage() orelse "no details" });
std.process.exit(1);
};
defer upload.deinit();
std.debug.print("image uploaded, image_id: {s}\n", .{upload.value.image_id});
std.debug.print("serpapi-zig {s} out-of-box test: success\n", .{serpapi.version});
}
Loading
Loading