# Creating a plugin proxy

To access authenticated external APIs with your [plugin](https://docs.cortex.io/streamline/plugins), you can configure a plugin proxy.

Plugin proxies try to match incoming requests against configured URL prefixes and apply rules to those requests (such as adding headers) before propagating the request to the specified downstream URL. Header rules have access to secrets via Mustache-like templating for easy authentication.

Plugin proxies must be explicitly associated with a particular plugin in the plugin configuration; once specified, all proxyFetch requests made by that plugin will use the proxy. If no proxy is defined for a plugin, no special logic will apply and requests will be propagated to downstream URLs without modification.

### Considerations

When creating a plugin proxy, consider the following:

* If there are multiple matching URL prefixes with overlapping header keys, the more specific (i.e. longer) prefix rules will be used.
* If a request from the plugin is invoked with header keys that overlap the proxy ruleset, the request-specific header values will be used.

## How to create a plugin proxy

Users with the `Edit plugin proxies` permission can create a plugin proxy.

1. If you are using a proxy with sensitive information, such as an access token, first follow the [steps to create a secret](https://docs.cortex.io/configure/settings/api-keys/secrets) for your proxy.
2. Navigate to **Plugins.** In the upper right corner, click **Create proxy**.
3. Configure the proxy:
   * **Proxy name**: Enter a human-readable name for the proxy.
   * **Path**: This field is automatically populated based on the name. It is a unique identifier for the proxy.
   * **URL prefixes**: Click **Add URL** to configure URL prefixes to apply rules to requests going through this proxy. All requests passing through this proxy that match the URL prefix will have a set of rules applied.
     * **Header**: Optionally configure a header for the URL.
       * If you are using a proxy with sensitive information, you can use Mustache-like templating to reference a global secret by tag, e.g., `{{secrets.my_auth_token}}`.
4. Click **Create proxy**.

See a sample plugin proxy configuration and sample plugin code below under [Example plugin proxy configuration](#example-plugin-proxy-configuration).

### Example plugin proxy configuration

```json
{
  "id": "22",
  "tag": "github",
  "urlConfigurations": {
    "https://api.github.com": {
      "headers": [
        { "key": "X-GitHub-API-Version", "value": "2022-11-28" },
        { "key": "Authorization", "value": "Bearer {{ secrets.github_read_token }}" }
      ]
    }
  }
}
```

#### Example plugin code

```ts
import { CortexApi } from "@cortexapps/plugin-core";

const getGithubReleases = async (ownerName: string, repoName: string) => {
  const githubReleases = await fetch(`https://api.github.com/repo/${ownerName}/${repoName}/releases`);
  return githubReleases.json();
};
```

In this scenario, the request to fetch GitHub requests will be sent with the `X-GitHub-API-Version` and `Authorization` headers, with the `Authorization` header value interpolated to include the `github_read_token` secret if it exists in your Cortex workspace.

## Manage plugin proxies

You can view your plugin proxies in **Plugins** under the [Proxies tab](https://app.getcortexapp.com/admin/plugins?activeTab=Proxies).

### Edit a proxy

1. Navigate to Plugins then click the [Proxies tab](https://app.getcortexapp.com/admin/plugins?activeTab=Proxies).
2. In the row containing the proxy, click the 3 dots icon.
3. In the dropdown that appears, click **Edit proxy**.\\

   <figure><img src="https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-c68cca0384c893b0d246b279ef645ca7443b6127%2Fedit-proxy.jpg?alt=media" alt=""><figcaption></figcaption></figure>
4. Make any desired changes, then click **Save proxy**.

### Delete a proxy

1. Navigate to Plugins then click the [Proxies tab](https://app.getcortexapp.com/admin/plugins?activeTab=Proxies).
2. In the row containing the proxy, click the 3 dots icon.
3. In the dropdown that appears, click **Delete proxy**.\\

   <figure><img src="https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-c68cca0384c893b0d246b279ef645ca7443b6127%2Fedit-proxy.jpg?alt=media" alt=""><figcaption></figcaption></figure>
4. In the confirmation modal, click **Delete**.


---

# 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://docs.cortex.io/streamline/plugins/creating-plugins/plugin-proxy.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.
