> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiJmNzcxNjRiZS1lZTY4LTQxODYtODE1OS1mOWE2OWY2NDM4ZmIiLCJleHAiOjE3NzgzNjM4OTcsImlhdCI6MTc3ODM2MzU5N30.h_G5sEUdX6gMMfiKSOJiJGFxm8vEHUQRms3ZK0aTKfw
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 添加自定义代码

> 使用自定义方法、逻辑和依赖项扩展 Fern 生成的 SDK。使用 .fernignore 保护您的代码不被重新生成时覆盖。

Fern 生成的 SDK 设计为可使用自定义逻辑、方法和依赖项进行扩展。如果您希望您的 SDK 不仅仅进行基本的 API 调用（比如组合多个调用、处理数据、添加工具类），您可以添加与生成代码和谐共存的自定义代码。

您还可以通过继承 Fern 生成的客户端并扩展它来添加自定义方法，并在您的 `generators.yml` 文件中添加您的自定义方法依赖的任何依赖项。

## 使用 `.fernignore` 保留您的自定义代码

一旦您添加了包含自定义代码的文件，使用 `.fernignore` 来保护您的自定义代码不被 Fern 重新生成 SDK 时覆盖。

只需将您的自定义文件添加到 SDK 仓库并在 `.fernignore` 中列出它们。Fern 不会覆盖其中列出的任何文件。当您使用 GitHub 发布时，会在您的 SDK 仓库中自动创建一个 `.fernignore` 文件。

<Note>
  `.fernignore`

   仅适用于 SDK 生成。它对 

  [Fern Docs](/learn/docs/getting-started/overview)

   构建没有影响。
</Note>

您的 `.fernignore` 文件可能看起来像这样：

```gitignore title=".fernignore"
# Documentation and licensing
README.md
LICENSE

# Custom workflows
.github/workflows/test.yml
.github/workflows/ci.yml

# Custom code
src/CustomClient.ts
```

<Note>
  有关另一个示例，请参阅 Cohere 的 

  [TypeScript SDK 的 `.fernignore` 文件](https://github.com/cohere-ai/cohere-typescript/blob/ad583e3003bd51e80a82317f9e16beec85881b86/.fernignore)

  。
</Note>

您的每个 SDK 都将有一个单独的 `.fernignore` 文件：

<Files>
  <Folder name="company-repo" defaultOpen comment="所有 SDK 的顶级仓库">
    <Folder name="fern">
      <Folder name="apis">
        <Folder name="sdks">
          <File name="generators.yml" comment="包含所有 SDK 的设置" />
        </Folder>
      </Folder>
    </Folder>
  </Folder>

  <Folder name="typescript-sdk-repo" defaultOpen comment="TypeScript SDK 的仓库">
    <File name=".fernignore" highlighted />

    <Folder name=".github">
      <Folder name="workflows" />
    </Folder>

    <Folder name="src" />
  </Folder>

  <Folder name="python-sdk-repo" defaultOpen>
    <File name=".fernignore" highlighted />

    <Folder name=".github">
      <Folder name="workflows" />
    </Folder>

    <Folder name="src" />
  </Folder>
</Files>

## 使用自定义代码增强您的 SDK

开始为特定 SDK 添加自定义代码：

<CardGroup cols={3}>
  <Card title="TypeScript" icon={<img src="https://files.buildwithfern.com/fern.docs.buildwithfern.com/learn/0c71a244f008622f56c81ae5e05d0f61952266394f4e3c39b37112357c18a71d/products/sdks/images/icons/ts-light.svg" alt="TypeScript logo" className="h-6 w-6" noZoom />} href="/sdks/generators/typescript/custom-code" />

  <Card title="Python" icon={<img src="https://files.buildwithfern.com/fern.docs.buildwithfern.com/learn/bb43fa5c5bfc13a89c4b824660665ef8a74d8b69c52ab8ddaf774818bd472d0d/products/sdks/images/icons/python-light.svg" className="h-6 w-6" noZoom />} href="/sdks/generators/python/custom-code" />

  <Card title="Go" icon={<img src="https://files.buildwithfern.com/fern.docs.buildwithfern.com/learn/db4418046239427ed4cffe87ed86155de27bb9fab8ef00eb0b83af90e7048ec0/products/sdks/images/icons/go-light.svg" className="h-6 w-6" noZoom />} href="/learn/sdks/generators/go/custom-code" />

  <Card title="Java" icon={<img src="https://files.buildwithfern.com/fern.docs.buildwithfern.com/learn/16f4cfd4606ca153b4a6c5a50bc3e0009eef9e0b751e11512488cb8d6fee4487/products/sdks/images/icons/java-light.svg" className="h-6 w-6" noZoom />} href="/learn/sdks/generators/java/custom-code" />

  <Card title=".NET" icon={<img src="https://files.buildwithfern.com/fern.docs.buildwithfern.com/learn/1c15a5ac85d820c2095bbac4c897b96270211013dcaeb0281be61290b001f22b/products/sdks/images/icons/csharp-light.svg" className="h-6 w-6" noZoom />} href="/sdks/generators/csharp/custom-code" />

  <Card title="PHP" icon={<img src="https://files.buildwithfern.com/fern.docs.buildwithfern.com/learn/789888f23fd4bdcd3188f91ca5bb99150d311576184f722dd35892471fdf5435/products/sdks/images/icons/php-light.svg" className="h-6 w-6" noZoom />} href="/sdks/generators/php/custom-code" />

  <Card title="Ruby" icon={<img src="https://files.buildwithfern.com/fern.docs.buildwithfern.com/learn/abdf63725ca23105103d293f65e64cf98180e23c58460ac40c2307a5227180a4/products/sdks/images/icons/ruby-light.svg" className="h-6 w-6" noZoom />} href="/sdks/generators/ruby/custom-code" />
</CardGroup>