# RESTful API

## 介绍

RESTful API 是一套控制 Clash 的一个途径，能获取 Clash 中的一些信息，同时也能控制 Clash 内部的配置。

基于 API，可以打造自己的可视化操作部分，也是实现 Clash GUI 的重要组成部分。

## 如何开启

在 Clash 的配置文件中加入 `external-controller` 字段，如：

```
# 监听在 127.0.0.1 的 8080 端口
external-controller = 127.0.0.1:8080

# 你可以加入 secret 进行 API 鉴权
# 鉴权的方式为在 Http Header 中加入 Authorization: Bearer ${secret}
secret = thisisyoursecret
```

{% hint style="warning" %}
&#x20;如果不是为了特殊需求，请尽量不要把 API 暴露在 0.0.0.0，如果真的要这么做，一定要加上 secret 进行鉴权
{% endhint %}

## CORS

为了能使 Clash 更加灵活，RESTful API 支持 CORS 让使用者能从浏览器使用 XHR、fetch 调用。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://clash.gitbook.io/doc/restful-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
