> ## 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.

# 搜索 Instagram 音频

> 在 Instagram 的授权音频目录中搜索可附加到 Reel 的曲目

export const InstagramAudioNotice = () => <Warning>
  <strong>Requires Instagram linked through Facebook Login.</strong> Instagram's Audio API is only available to accounts connected via <strong>Facebook Login</strong>, which needs a Facebook Page connected to the Instagram account. Accounts connected with <strong>Instagram Login</strong> (the Ayrshare default) cannot search, look up, or attach audio, and every audio request returns <code>code: 514</code>.
  <br /><br />
  This is a Meta restriction, not an Ayrshare one. Meta's documentation states the Audio API "is only available on the Instagram API with Facebook Login. It is not supported on the Instagram API with Instagram Login." No permission or plan upgrade changes it.
  <br /><br />
  <strong>Three things are required:</strong>
  <br />
  1. An Instagram <strong>Business</strong> or <strong>Creator</strong> account<br />
  2. A <strong>Facebook Page connected</strong> to that Instagram account<br />
  3. Instagram linked to Ayrshare with <strong>Instagram Login disabled</strong>, so the Facebook Login flow is used
  <br /><br />
  See the <a href="/docs/dashboard/connect-social-accounts/instagram#music-attach-eligibility-facebook-login-required">step-by-step setup guide</a>.
</Warning>;

export const PlansAvailable = ({plans = [], maxPackRequired}) => {
  let displayPlans = plans;
  if (plans && plans.length === 1) {
    const lowerCasePlan = plans[0].toLowerCase();
    if (lowerCasePlan === "business") {
      displayPlans = ["Launch", "Business", "Enterprise"];
    } else if (lowerCasePlan === "premium") {
      displayPlans = ["Premium", "Launch", "Business", "Enterprise"];
    }
  }
  return <Note>
Available on {displayPlans.length === 1 ? "the " : ""}
{displayPlans.join(", ").replace(/\b\w/g, l => l.toUpperCase())}{" "}
{displayPlans.length > 1 ? "plans" : "plan"}.

{maxPackRequired && <span onClick={() => window.open('https://www.ayrshare.com/docs/additional/maxpack', '_self')} className="flex items-center mt-2 cursor-pointer">
 <span className="px-1.5 py-0.5 rounded text-sm" style={{
    backgroundColor: '#C264B6',
    color: 'white',
    fontSize: '12px'
  }}>
   Max Pack required
 </span>
</span>}
</Note>;
};

export const HeaderAPI = ({noProfileKey, profileKeyRequired}) => <>
    <ParamField header="Authorization" type="string" required>
      <a href="/docs/apis/overview#authorization">API Key</a> of the Primary Profile.
      <br />
      <br />
      Format: <code>Authorization: Bearer API_KEY</code>
    </ParamField>
    {!noProfileKey && (profileKeyRequired ? <ParamField header="Profile-Key" type="string" required>
          <a href="/docs/apis/overview#profile-key-format">Profile Key</a> of a User Profile.
          <br />
          <br />
          Format: <code>Profile-Key: PROFILE_KEY</code>
        </ParamField> : <ParamField header="Profile-Key" type="string">
          <a href="/docs/apis/overview#profile-key-format">Profile Key</a> of a User Profile.
          <br />
          <br />
          Format: <code>Profile-Key: PROFILE_KEY</code>
        </ParamField>)}
  </>;

<PlansAvailable plans={["premium"]} maxPackRequired={false} />

按关键词搜索 Instagram 的音频目录。将返回的 `audioId` 与 [`instagramOptions.audioConfiguration`](/docs/apis/post/social-networks/instagram#adding-music-to-a-reel) 一起使用，即可在发布 Reel 时附加该曲目。

<InstagramAudioNotice />

## Header Parameters

<HeaderAPI />

## Query Parameters

<ParamField query="query" type="string" required>
  要搜索的关键词，例如艺人或曲目名称。空值或仅包含空白字符的值会返回
  `code: 101`。
</ParamField>

<ParamField query="audioType" type="string" default="music">
  要搜索的目录。`music` 表示已授权的商业曲目，`original_sound` 表示取自
  其他创作者 Reels 的音频。任何其他值都会返回 `code: 101`。
</ParamField>

<ParamField query="limit" type="integer">
  返回曲目的最大数量，取值范围为 `1` 到 `100`。非整数或超出范围的值会返回
  `code: 101`。省略时使用 Instagram 自身的默认值。
</ParamField>

## 响应字段

Instagram 不会为每首曲目返回所有字段，因此请将除 `audioId` 以外的所有曲目字段都视为可选。

<ResponseField name="status" type="string">
  搜索完成时为 `success`。目录中没有匹配结果仍视为成功。
</ResponseField>

<ResponseField name="query" type="string">
  被搜索的关键词，原样返回。
</ResponseField>

<ResponseField name="audio" type="array">
  匹配的曲目。空数组表示搜索已执行但没有任何匹配。

  <Expandable title="曲目字段">
    <ResponseField name="audioId" type="string">
      发布 Reel 时作为 `audioConfiguration.audioId` 传入的 id。
    </ResponseField>

    <ResponseField name="title" type="string">曲目标题。</ResponseField>
    <ResponseField name="displayArtist" type="string">Instagram 所显示的艺人名称。</ResponseField>
    <ResponseField name="durationMs" type="integer">曲目时长（毫秒）。</ResponseField>
    <ResponseField name="thumbnailUrl" type="string">封面图缩略图 URL。</ResponseField>
    <ResponseField name="audioType" type="string">`music` 或 `original_sound`。</ResponseField>

    <ResponseField name="isAdsEligible" type="boolean">
      该曲目是否可用于广告。授权音乐即便可用于自然发布的 Reel，
      也常常无法用于广告。
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseExample>
  ```json 200: Success theme={"system"}
  {
    "status": "success",
    "query": "summer",
    "audio": [
      {
        "audioId": "17901234567890123",
        "title": "Summer Nights",
        "displayArtist": "The Example Band",
        "durationMs": 32000,
        "thumbnailUrl": "https://scontent.example.com/cover.jpg",
        "audioType": "music",
        "isAdsEligible": true
      }
    ]
  }
  ```

  ```json 200: No matches theme={"system"}
  {
    "status": "success",
    "query": "asdfghjkl",
    "audio": []
  }
  ```

  ```json 400: Instagram Login account theme={"system"}
  {
    "status": "error",
    "code": 514,
    "action": "post",
    "message": "Adding music to a Reel requires an Instagram account connected via Facebook Login. This profile is connected with Instagram Login, which Meta does not permit for music attach, search, or lookup. Please relink Instagram using Facebook Login and grant the instagram_content_publish permission, then try again.",
    "platform": "instagram"
  }
  ```
</ResponseExample>

## 错误代码

| 代码  | 含义                                                           |
| --- | ------------------------------------------------------------ |
| 101 | `query` 缺失或为空、`audioType` 无法识别，或 `limit` 超出 1 到 100 的范围。     |
| 156 | 该配置未关联 Instagram。                                            |
| 161 | Instagram 的授权已过期或被撤销。请重新关联账号。                                |
| 239 | Instagram 因账号层面的原因拒绝了请求，包括曲目不可用。在搜索时报告的是 239，而不是查询端点返回的 517。 |
| 258 | Instagram 返回了格式错误的响应或未映射的失败。                                 |
| 425 | 该连接缺少所需的 Instagram 权限。请重新关联并授予所有权限。                          |
| 435 | 已达到 Instagram 的速率限制。请稍候再重试。                                  |
| 514 | Instagram 通过 Instagram Login 关联。Audio API 需要 Facebook Login。 |

代码 514 到 518 的说明见 [Instagram Reels 音频错误](/docs/errors/errors-ayrshare#instagram-reels-audio-errors)；其余代码见 [Ayrshare 错误参考](/docs/errors/errors-ayrshare)。

## 后续步骤

<Card title="将曲目附加到 Reel" icon="music" href="/docs/apis/post/social-networks/instagram#adding-music-to-a-reel" horizontal />

<Card title="查询单个曲目" icon="magnifying-glass" href="/docs/apis/media/get-instagram-audio" horizontal />
