Case 1: The "Wrong Currency" Issue

Problem Statement

Scenario: A user in Jakarta gets prices in USD because they rely on IP geolocation instead of explicit parameters.

Common Mistake

Relying solely on the location parameter often returns USD currency because the Google server IP might be in the US.

The Broken Code

Here's what users typically try first:

broken.js
import * as Dotenv from "dotenv";
import { getJson } from "serpapi";

Dotenv.config();

console.log("Fetching prices for Iphone 15 in Jakarta...");

// BROKEN LOGIC:
// Relying solely on 'location' string often returns USD currency
// because the Google server IP might be in the US.
const json = await getJson({
  engine: "google_shopping",
  api_key: process.env.SERPAPI_KEY,
  q: "Iphone 15",
  location: "Jakarta", // Missing 'gl' and 'hl'
});

try {
  const item = json.shopping_results[0];
  console.log(`Result: ${item.title}`);
  console.log(`Price: ${item.price}`); // Likely shows USD (e.g., "$799") instead of IDR
} catch (error) {
  console.log("No results found.");
}

Output:

Price: $799 [Wrong] Should be in IDR (Rp)

The Root Cause

While the location parameter is important, it's not always enough to force Google to switch the currency and language context entirely. You might get Jakarta location data, but still see prices in English or mixed currencies.

The Solution

To get accurate data, you need to be explicit. Use the Holy Trinity of Parameters:

  1. location: The physical location (e.g., "Jakarta, Indonesia")
  2. gl: The Country Code (e.g., id for Indonesia) - dictates the region for search interest
  3. hl: The Language Code (e.g., id for Bahasa Indonesia) - ensures UI text and currency formatting
solution.js
import * as Dotenv from "dotenv";
import { getJson } from "serpapi";

Dotenv.config();

// Technical Note: Using Top-level await (Node.js 14+)
try {
  console.log("Fetching localized prices for Indonesia...");

  const json = await getJson({
    engine: "google_shopping",
    api_key: process.env.SERPAPI_KEY,
    q: "Iphone 15",
    location: "Jakarta, Indonesia",
    gl: "id", // Parameter: Country (Indonesia)
    hl: "id", // Parameter: Language (Indonesian)
  });

  if (json.shopping_results) {
    const item = json.shopping_results[0];
    console.log(`Found: ${item.title}`);
    console.log(`Price: ${item.price}`); // Should be in IDR (Rp)
  } else {
    console.log("No shopping results found.");
  }
} catch (error) {
  console.error("API Error:", error.message);
}

Output:

Price: Rp 15.999.000 [Correct]

Key Takeaways

Best Practice

Always combine location, gl, and hl parameters for accurate localized results.

  • Use google_shopping engine for structured product data
  • Add gl parameter to enforce country context
  • Add hl parameter to enforce language/currency formatting
  • Include defensive checks for API errors and empty results