---
title: "API クライアントと SDK: サンプルのアドレスを残さない"
description: "api.example-petstore.com が 410 Gone を返す理由、ベース URL を設定で管理する方法、サンプルのアドレスが本番環境に届かないようにする方法を説明します。"
url: https://example-petstore.com/ja/guides/api-base-url
language: ja
---

# API クライアントと SDK の設定

api.example-petstore.com などのアドレスへのリクエストは、サンプルのアドレスを今もベース URL として使っているコードから送られています。このガイドでは、そのアドレスがよく設定されている場所、設定へ移す方法、再発を防ぐ方法を説明します。

## 返されるレスポンス

本文付きのリクエスト、`/v2/pet` などの API 形式のパスへのリクエスト、JSON を要求するリクエストには、 問題の詳細（RFC 9457）とともに `410 Gone` を返します。

```
HTTP/1.1 410 Gone
Content-Type: application/problem+json; charset=utf-8

{"type":"https://example-petstore.com/#where","title":"Example domain, not a real service",
 "status":410,"detail":"api.example-petstore.com is an example domain used in documentation. …"}
```

このドメインは、[`petstore.swagger.io`](https://example-petstore.com/ja/guides/swagger-petstore) にある Swagger Petstore サンプル API ではありません。

## アドレスの設定場所

- コード内の定数やデフォルト値（`BASE_URL = "https://api.example-petstore.com"`）
- サンプルからコピーした設定ファイル、`.env` ファイル、環境変数
- クライアントの生成に使用した OpenAPI 定義の `host` フィールドまたは `servers` フィールド
- `{{baseUrl}}` などの Postman や Insomnia の環境変数
- プレースホルダに対して実行されるテスト、フィクスチャ、CI ジョブ

## 正しく設定する

アドレスは設定から読み込み、設定されていない場合は明確なエラーで停止するようにします。

```
# Python: read the address from configuration, not from the code
import os
BASE_URL = os.environ["API_BASE_URL"]

// JavaScript / Node.js
const baseURL = process.env.API_BASE_URL;
```

```
# Python, httpx
client = httpx.Client(base_url=os.environ["API_BASE_URL"])

// Node.js, axios
const api = axios.create({ baseURL: process.env.API_BASE_URL });

# Generated OpenAPI client (Python)
configuration = Configuration(host=os.environ["API_BASE_URL"])
```

Postman や Insomnia では、環境ごとに `baseUrl` を設定し、送信前に正しい環境を選択します。

## 再発を防ぐ

起動時またはテスト時に、サンプルのアドレスを拒否するチェックを追加します。

```
import os, re
base = os.environ["API_BASE_URL"]
if re.search(r"example-(petstore|commerce-host)\.com", base):
    raise RuntimeError(f"API_BASE_URL still points at an example domain: {base}")
```

自社のドキュメントやサンプルでは、`api.example.com` など、その目的のために予約された名前を使用してください。 詳しくは[サンプル ドメイン](https://example-petstore.com/ja/example-domains)をご覧ください。

## 送信されたキー

リクエストに API キー、トークン、パスワード、セッション Cookie が含まれていた場合、それらは誤ったサーバーに届いています。 発行したサービスで無効にし、新しいものを発行してください。[漏えいした認証情報: 今すぐ行うべきこと](https://example-petstore.com/ja/guides/leaked-credentials)

本物のサービスを使わずにテストするには: [サンプルのアドレスではなく Mock API でテストする](https://example-petstore.com/ja/guides/mock-api)

## 出典

- [RFC 9457: Problem Details for HTTP APIs](https://www.rfc-editor.org/rfc/rfc9457.html) IETF
- [Secrets Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html) OWASP
- [Swagger Petstore sample API](https://petstore.swagger.io/) Swagger
