> ## Documentation Index
> Fetch the complete documentation index at: https://www.ayrshare.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 城市

> 依名稱搜尋可用於推廣廣告定位的 Facebook 城市，結果附帶所屬地區 region 與 regionId，以及 supportsCity 等定位標記。

搜索可在推廣廣告時用於定位的城市。

<ul>
  <li>
    城市使用 Facebook 的 `key` 標識符，並支援半徑定位。
  </li>

  <li>
    每條城市結果會將其所屬地區作為 `region`（地區名稱）與 `regionId`（地區數字標識符）一並返回，
    因此當所屬地區可用時，您可以對該城市所屬的地區進行定位。
  </li>

  <li>
    如果返回的城市中 `supportsRegion` 為 `true`，則該城市所屬地區可用於定位。
    如果 `supportsCity` 為 `true`，則該城市本身可用於定位。
  </li>

  <li>
    某些地點可能以 `subcity` 形式返回，並附帶額外的層級欄位。
  </li>
</ul>

## 請求頭參數

**Authorization** `string` *required*

[API Key](https://www.ayrshare.com/docs/apis/overview.md#authorization) of the Primary Profile.

Format: `Authorization: Bearer API_KEY`
**Profile-Key** `string`

[Profile Key](https://www.ayrshare.com/docs/apis/overview.md#profile-key-format) of a User Profile.

Format: `Profile-Key: PROFILE_KEY`

## 查詢參數

**search** `string`

用於過濾城市的搜索字串。可提供部分或完整的城市名稱。

**limit** `number` (default: 25)

返回的城市最大數量。

```bash cURL theme={"system"}
curl \
-H "Authorization: Bearer API_KEY" \
-X GET "https://api.ayrshare.com/api/ads/facebook/cities?search=Manhattan"
```

```javascript JavaScript theme={"system"}
const API_KEY = "API_KEY";

fetch("https://api.ayrshare.com/api/ads/facebook/cities?search=Manhattan", {
      method: "GET",
      headers: {
        "Authorization": `Bearer ${API_KEY}`
      }
    })
      .then((res) => res.json())
      .then((json) => console.log(json))
      .catch(console.error);
```

```python Python theme={"system"}
import requests

headers = {'Authorization': 'Bearer API_KEY'}

r = requests.get('https://api.ayrshare.com/api/ads/facebook/cities?search=Manhattan', headers=headers)

print(r.json())
```

```php PHP theme={"system"}
<?php
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.ayrshare.com/api/ads/facebook/cities?search=Manhattan",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer API_KEY"
  ],
]);

$response = curl_exec($curl);
curl_close($curl);

echo $response;
?>
```

```csharp C# theme={"system"}
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", "Bearer API_KEY");

        var response = await client.GetAsync("https://api.ayrshare.com/api/ads/facebook/cities?search=Manhattan");
        var content = await response.Content.ReadAsStringAsync();

        Console.WriteLine(content);
    }
}
```

```go Go theme={"system"}
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://api.ayrshare.com/api/ads/facebook/cities?search=Manhattan", nil)
    req.Header.Add("Authorization", "Bearer API_KEY")

    resp, _ := client.Do(req)
    body, _ := ioutil.ReadAll(resp.Body)

    fmt.Println(string(body))
}
```

```java Java theme={"system"}
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class GetAdCities {
    public static void main(String[] args) throws Exception {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.ayrshare.com/api/ads/facebook/cities?search=Manhattan"))
                .header("Authorization", "Bearer API_KEY")
                .GET()
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
```

```ruby Ruby theme={"system"}
require 'net/http'
require 'json'

uri = URI('https://api.ayrshare.com/api/ads/facebook/cities?search=Manhattan')
req = Net::HTTP::Get.new(uri)
req['Authorization'] = 'Bearer API_KEY'

res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http|
  http.request(req)
}

puts res.body
```

```json 200: Success theme={"system"}
{
  "status": "success",
  "cities": [
    {
      "key": "2447439",
      "name": "Manhattan",
      "type": "city",
      "countryCode": "US",
      "countryName": "United States",
      "region": "Kansas",
      "regionId": 3859,
      "supportsRegion": true,
      "supportsCity": true
    },
    {
      "key": "2703980",
      "name": "Manhattan",
      "type": "subcity",
      "countryCode": "US",
      "countryName": "United States",
      "region": "New York",
      "regionId": 3875,
      "supportsRegion": true,
      "supportsCity": true,
      "geoHierarchyLevel": "SUBCITY",
      "geoHierarchyName": "BOROUGH"
    }
  ],
  "count": 2
}
```

```json 414: Location metadata error theme={"system"}
{
  "action": "ads",
  "status": "error",
  "code": 414,
  "message": "Error getting ad location metadata. Please try again."
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.