# Calling internal service endpoints via plugin proxy

Your organization may have services that are not publicly accessible but contain information that would be useful to manage in Cortex. You can set up a plugin proxy that works with [Cortex's Axon Relay](https://docs.cortex.io/ingesting-data-into-cortex/integrations/axon-relay) to call internal services without exposing the service or authentication outside of your firewall.

## How to call internal service endpoints in a plugin proxy

### Prerequisites

Before getting started:

* You should have already [set up the Cortex Axon agent](https://docs.cortex.io/ingesting-data-into-cortex/integrations/axon-relay) and [set your Cortex API token in the .env file](https://docs.cortex.io/ingesting-data-into-cortex/integrations/axon-relay#step-1.2-create-a-.env-file-and-a-docker-compose.yml-file).
* You should have already followed the steps to [create a plugin](https://docs.cortex.io/streamline/plugins/creating-plugins) and a [plugin proxy](https://docs.cortex.io/streamline/plugins/creating-plugins/plugin-proxy).

#### Considerations

* This guide describes using a Docker compose file, but note that Axon Relay can be run in Kubernetes or other container systems.
* As an example throughout this guide, assume a service in the network has the following endpoint:

`GET http://cities.internal.my-company.com/api/cities/s => ["Seattle", "Sydney", "Shanghai" ...]`

### Step 1: Create request forwarding configuration

* Create a file that defines how requests are routed.
  * The example below permits any requests, but note that the `method` and `path` can be narrowed to allow access to specific endpoints:

```json
{
   "private": [
     {
       "method": "any", 
       "path": "/*",   
       "origin": "http://cities.internal.my-company.com"
     }
   ]
}

```

In your configuration, you will enter the root of your service URL as the value of `origin`.

### Step 2: Configure Axon Docker container

1. Create the Axon container (in this example, via Docker Compose for brevity) that defines how the container will route traffic and which routes are allowed.
   * For production employments, two replicas should be specified, and the instances should be run in Kubernetes or some other durable system.. The example below can be used to test the configuration and can be run locally.

```yaml
services:
 relay-cities:
   image: ghcr.io/cortexapps/cortex-axon-agent:latest
   environment:
     HOSTNAME: relay-cities-${HOSTNAME:-local}
     CORTEX_API_TOKEN: $CORTEX_API_TOKEN
   volumes:
     - "./accept.cities.json:/app/accept.json"
   command: [
     "relay",
     "-a", "axon-relay-cities", # this can be any unique value
     "-f", "/app/accept.json",
   ]

```

2. Start the container, which will register it with Cortex: Run the command `docker compose up`.\\

### Step 3: Include the Axon call in your plugin proxy code

Your endpoints can be invoked by specifying a URL in the format: `http://[your-configuration-alias]@relay-proxy.cortex.io[/desired/url/path]`

For the example above, we could create a helper function to fetch cities by prefix:

```javascript
import from "@cortexapps/plugin-core";

const getCitiesByLetter = async (prefix: string) => { 
  const result = await fetch(`http://axon-relay-cities@relay-proxy.cortex.io/api/cities?prefix=${prefix}`); 
  return result.json(); 
};
```


---

# 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/calling-internal-service-endpoints-via-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.
