快速开始
几分钟内获取 XAU/USD 实时价格。
开始之前
你需要 curl 或任意 HTTP 客户端,以及一个免费 API 密钥。注册并确认邮箱,然后从账户复制密钥。免费套餐包含每小时 100 次请求。
第一个请求
在 X-API-Key 请求头中发送密钥。请把密钥放在服务器上,不要放在访客能看到的代码里。
curl --fail-with-body --max-time 10 "https://goldapi.dev/api/v1/price/XAU/USD" \ -H "X-API-Key: $API_KEY"const res = await fetch("https://goldapi.dev/api/v1/price/XAU/USD", { headers: { "X-API-Key": process.env.API_KEY }, signal: AbortSignal.timeout(10_000),});if (!res.ok) throw new Error("HTTP " + res.status);console.log(await res.json());import os, requestsres = requests.get( "https://goldapi.dev/api/v1/price/XAU/USD", headers={"X-API-Key": os.environ["API_KEY"]}, timeout=10,)res.raise_for_status()print(res.json())package mainimport ( "fmt" "io" "net/http" "os" "time")func main() { client := http.Client{Timeout: 10 * time.Second} req, err := http.NewRequest("GET", "https://goldapi.dev/api/v1/price/XAU/USD", nil) if err != nil { panic(err) } req.Header.Set("X-API-Key", os.Getenv("API_KEY")) res, err := client.Do(req) if err != nil { panic(err) } defer res.Body.Close() body, err := io.ReadAll(res.Body) if err != nil { panic(err) } if res.StatusCode != 200 { panic(fmt.Sprintf("HTTP %d: %s", res.StatusCode, body)) } fmt.Println(string(body))}import java.io.IOException;import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.time.Duration;public class Main { public static void main(String[] args) throws Exception { HttpRequest request = HttpRequest.newBuilder(URI.create("https://goldapi.dev/api/v1/price/XAU/USD")) .header("X-API-Key", System.getenv("API_KEY")) .timeout(Duration.ofSeconds(10)) .build(); HttpResponse<String> response = HttpClient.newHttpClient() .send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() != 200) throw new IOException("HTTP " + response.statusCode()); System.out.println(response.body()); }}import Foundationvar request = URLRequest(url: URL(string: "https://goldapi.dev/api/v1/price/XAU/USD")!)request.setValue(ProcessInfo.processInfo.environment["API_KEY"], forHTTPHeaderField: "X-API-Key")request.timeoutInterval = 10let (data, response) = try await URLSession.shared.data(for: request)guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw URLError(.badServerResponse) }print(String(data: data, encoding: .utf8)!)$context = stream_context_create(["http" => [ "header" => "X-API-Key: " . getenv("API_KEY"), "timeout" => 10,]]);$body = file_get_contents("https://goldapi.dev/api/v1/price/XAU/USD", false, $context);if ($body === false) throw new RuntimeException("Request failed");echo $body;读取响应
price 是响应中单位的价格:金衡盎司,铜为磅。gram 是响应中 K 数对应的每克价格,updatedAt 是生成响应的时间。
响应 200
{
"symbol": "XAU",
"name": "Gold",
"currency": "USD",
"unit": "oz",
"price": 4072.1,
"gram": 130.921,
"karat": 24,
"updatedAt": "2026-11-07T02:43:29+02:00"
}
出错时
错误是带 error 字段的 JSON。常见的有缺少请求头时的 401 api_key_required、密钥错误时的 401 invalid_api_key,以及确认邮箱前的 403 email_not_verified。超过限制会收到带 Retry-After 响应头的 429 rate_limit_exceeded。