> This page is for SDK, version Current (default).
> For other versions, use one of these documentation indexes:
> - Current (default): https://buildwithfern.com/learn/sdks/current/llms.txt
> - Legacy: https://buildwithfern.com/learn/sdks/legacy/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.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/zh/docs/getting-started/capabilities) 构建没有影响。

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

**`.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)。

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

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

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

#### [TypeScript](/learn/zh/sdks/generators/typescript/custom-code)

#### [Python](/learn/zh/sdks/generators/python/custom-code)

#### [Go](/learn/zh/sdks/generators/go/custom-code)

#### [Java](/learn/zh/sdks/generators/java/custom-code)

#### [.NET](/learn/zh/sdks/generators/csharp/custom-code)

#### [PHP](/learn/zh/sdks/generators/php/custom-code)

#### [Ruby](/learn/zh/sdks/generators/ruby/custom-code)