> ## Documentation Index
> Fetch the complete documentation index at: https://docs.glider.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# Portfolio Automation

> Endpoints for automating portfolio rebalancing

The Portfolio Automation endpoints allow you to start, pause, resume, and manage automated rebalancing of portfolios.

## Start Portfolio Automation

Starts automated rebalancing for a portfolio.

<ParamField path="portfolioId" type="string" required>
  The unique identifier of the portfolio
</ParamField>

<ParamField body="runRebalanceImmediately" type="boolean" default="false">
  Whether to run an initial rebalance immediately after starting
</ParamField>

<ParamField body="initializeScheduleAutomatically" type="boolean" default="true">
  Set to false for manual-only rebalancing without a schedule
</ParamField>

<ParamField body="interval" type="number" default="21600000">
  Rebalance interval in milliseconds (default: 6 hours)
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url 'https://api.glider.fi/v1/portfolio/port_abc123/rebalance/schedule/start' \
    --header 'Content-Type: application/json' \
    --header 'X-API-KEY: your_api_key_here' \
    --data '{
      "runRebalanceImmediately": true,
      "initializeScheduleAutomatically": true,
      "interval": 21600000
    }'
  ```

  ```javascript JavaScript theme={null}
  const portfolioId = "port_abc123";
  const startResponse = await fetch(
    `https://api.glider.fi/v1/portfolio/${portfolioId}/rebalance/schedule/start`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-API-KEY": "your_api_key_here",
      },
      body: JSON.stringify({
        runRebalanceImmediately: true, // Run initial rebalance immediately
        initializeScheduleAutomatically: true, // Set to false for manual-only rebalancing
        interval: 21600000, // 6 hours in milliseconds
      }),
    }
  );

  const startResult = await startResponse.json();
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "port_abc123",
      "rebalanceScheduleId": "sched_abc123",
      "status": "started",
      "message": "Portfolio automation started successfully",
      "runRebalanceImmediately": true,
      "isAutomated": true,
      "intervalMs": 21600000
    },
    "correlationId": "corr_abc123",
    "requestId": "req_xyz789",
    "timestamp": "2023-05-21T13:45:12.345Z"
  }
  ```
</ResponseExample>

## Pause Portfolio Automation

Pauses automated rebalancing for a portfolio.

<ParamField path="portfolioId" type="string" required>
  The unique identifier of the portfolio
</ParamField>

<ParamField body="reason" type="string">
  Optional reason for pausing
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url 'https://api.glider.fi/v1/portfolio/port_abc123/rebalance/schedule/pause' \
    --header 'Content-Type: application/json' \
    --header 'X-API-KEY: your_api_key_here' \
    --data '{
      "reason": "Pausing for market volatility"
    }'
  ```

  ```javascript JavaScript theme={null}
  const portfolioId = "port_abc123";
  const pauseResponse = await fetch(
    `https://api.glider.fi/v1/portfolio/${portfolioId}/rebalance/schedule/pause`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-API-KEY": "your_api_key_here",
      },
      body: JSON.stringify({
        reason: "Pausing for market volatility", // Optional reason
      }),
    }
  );

  const pauseResult = await pauseResponse.json();
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "port_abc123",
      "scheduleId": "sched_abc123",
      "status": "paused",
      "message": "Portfolio automation paused successfully",
      "paused": true,
      "pausedAt": "2023-05-21T13:45:12.345Z",
      "reason": "Paused via API: Pausing for market volatility"
    },
    "correlationId": "corr_abc123",
    "requestId": "req_xyz789",
    "timestamp": "2023-05-21T13:45:12.345Z"
  }
  ```
</ResponseExample>

## Resume Portfolio Automation

Resumes previously paused automated rebalancing for a portfolio.

<ParamField path="portfolioId" type="string" required>
  The unique identifier of the portfolio
</ParamField>

<ParamField body="reason" type="string">
  Optional reason for resuming
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url 'https://api.glider.fi/v1/portfolio/port_abc123/rebalance/schedule/resume' \
    --header 'Content-Type: application/json' \
    --header 'X-API-KEY: your_api_key_here' \
    --data '{
      "reason": "Market stabilized"
    }'
  ```

  ```javascript JavaScript theme={null}
  const portfolioId = "port_abc123";
  const resumeResponse = await fetch(
    `https://api.glider.fi/v1/portfolio/${portfolioId}/rebalance/schedule/resume`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-API-KEY": "your_api_key_here",
      },
      body: JSON.stringify({
        reason: "Market stabilized", // Optional reason
      }),
    }
  );

  const resumeResult = await resumeResponse.json();
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "port_abc123",
      "scheduleId": "sched_abc123",
      "status": "active",
      "message": "Portfolio automation resumed successfully",
      "paused": false,
      "resumedAt": "2023-05-21T13:45:12.345Z",
      "reason": "Resumed via API: Market stabilized"
    },
    "correlationId": "corr_abc123",
    "requestId": "req_xyz789",
    "timestamp": "2023-05-21T13:45:12.345Z"
  }
  ```
</ResponseExample>

## Manually Trigger Rebalance

Manually triggers a portfolio rebalance.

<ParamField path="portfolioId" type="string" required>
  The unique identifier of the portfolio
</ParamField>

<ParamField body="skipScheduleValidation" type="boolean" default="false">
  Set to true to trigger a rebalance even if no schedule exists
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url 'https://api.glider.fi/v1/portfolio/port_abc123/rebalance/schedule/trigger' \
    --header 'Content-Type: application/json' \
    --header 'X-API-KEY: your_api_key_here' \
    --data '{
      "skipScheduleValidation": false
    }'
  ```

  ```javascript JavaScript theme={null}
  const portfolioId = "port_abc123";
  const triggerResponse = await fetch(
    `https://api.glider.fi/v1/portfolio/${portfolioId}/rebalance/schedule/trigger`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-API-KEY": "your_api_key_here",
      },
      body: JSON.stringify({
        skipScheduleValidation: false, // Set to true to auto-create schedules if needed
      }),
    }
  );

  const triggerResult = await triggerResponse.json();
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "port_abc123",
      "scheduleId": "sched_abc123",
      "rebalanceId": "rebal_def456",
      "status": "triggered",
      "message": "Portfolio rebalance triggered successfully",
      "triggerTime": 1684734312
    },
    "correlationId": "corr_abc123",
    "requestId": "req_xyz789",
    "timestamp": "2023-05-21T13:45:12.345Z"
  }
  ```
</ResponseExample>

## Check Automation Status

Checks the status of portfolio automation.

<ParamField path="portfolioId" type="string" required>
  The unique identifier of the portfolio
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request GET \
    --url 'https://api.glider.fi/v1/portfolio/port_abc123/automation/status' \
    --header 'X-API-KEY: your_api_key_here'
  ```

  ```javascript JavaScript theme={null}
  const portfolioId = "port_abc123";
  const statusResponse = await fetch(
    `https://api.glider.fi/v1/portfolio/${portfolioId}/automation/status`,
    {
      method: "GET",
      headers: {
        "X-API-KEY": "your_api_key_here",
      },
    }
  );

  const status = await statusResponse.json();
  console.log(`Automation status: ${status.data.status}`);
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "port_abc123",
      "status": "active", // or "paused", "inactive", "archived", "error"
      "hasAutomation": true,
      "message": "Portfolio automation is active",
      "rebalanceScheduleId": "sched_abc123",
      "details": {
        "totalExecutions": 5,
        "paused": false,
        "pauseReason": null,
        "recentActions": [
          {
            "workflowId": "rebal_def456",
            "takenAt": "2023-05-21T13:30:00.000Z",
            "scheduledAt": "2023-05-21T13:30:00.000Z"
          },
          {
            "workflowId": "rebal_ghi789",
            "takenAt": "2023-05-21T07:30:00.000Z",
            "scheduledAt": "2023-05-21T07:30:00.000Z"
          }
        ],
        "nextScheduledExecution": "2023-05-21T19:30:00.000Z"
      }
    },
    "correlationId": "corr_abc123",
    "requestId": "req_xyz789",
    "timestamp": "2023-05-21T13:45:12.345Z"
  }
  ```
</ResponseExample>

## Check Rebalance Status

Checks the status of portfolio rebalancing.

<ParamField path="portfolioId" type="string" required>
  The unique identifier of the portfolio
</ParamField>

<ParamField query="limit" type="number" default="5">
  Number of recent workflows to return
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request GET \
    --url 'https://api.glider.fi/v1/portfolio/port_abc123/rebalance/status?limit=5' \
    --header 'X-API-KEY: your_api_key_here'
  ```

  ```javascript JavaScript theme={null}
  const portfolioId = "port_abc123";
  const rebalanceStatusResponse = await fetch(
    `https://api.glider.fi/v1/portfolio/${portfolioId}/rebalance/status?limit=5`,
    {
      method: "GET",
      headers: {
        "X-API-KEY": "your_api_key_here",
      },
    }
  );

  const rebalanceStatus = await rebalanceStatusResponse.json();
  console.log(`Rebalance status: ${rebalanceStatus.data.status}`);
  console.log(`Recent workflows: ${rebalanceStatus.data.recentWorkflows.length}`);
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "port_abc123",
      "status": "active", // or "paused", "inactive", "error"
      "message": "Rebalance schedule is active",
      "rebalanceScheduleId": "sched_abc123",
      "details": {
        "totalExecutions": 5,
        "paused": false,
        "pauseReason": null,
        "nextScheduledExecution": "2023-05-21T19:30:00.000Z"
      },
      "recentWorkflows": [
        {
          "workflowId": "rebal_def456",
          "runId": "run_jkl012",
          "scheduledAt": "2023-05-21T13:30:00.000Z",
          "startedAt": "2023-05-21T13:30:00.123Z",
          "status": "triggered"
        },
        {
          "workflowId": "rebal_ghi789",
          "runId": "run_mno345",
          "scheduledAt": "2023-05-21T07:30:00.000Z",
          "startedAt": "2023-05-21T07:30:00.456Z",
          "status": "triggered"
        }
      ]
    },
    "correlationId": "corr_abc123",
    "requestId": "req_xyz789",
    "timestamp": "2023-05-21T13:45:12.345Z"
  }
  ```
</ResponseExample>

## Check Specific Rebalance Execution

Gets detailed information about a specific rebalance execution.

<ParamField path="portfolioId" type="string" required>
  The unique identifier of the portfolio
</ParamField>

<ParamField path="workflowId" type="string" required>
  The ID of the rebalance workflow execution
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request GET \
    --url 'https://api.glider.fi/v1/portfolio/port_abc123/rebalance/status/rebal_def456' \
    --header 'X-API-KEY: your_api_key_here'
  ```

  ```javascript JavaScript theme={null}
  const portfolioId = "port_abc123";
  const workflowId = "rebal_def456";
  const rebalanceDetailsResponse = await fetch(
    `https://api.glider.fi/v1/portfolio/${portfolioId}/rebalance/status/${workflowId}`,
    {
      method: "GET",
      headers: {
        "X-API-KEY": "your_api_key_here",
      },
    }
  );

  const rebalanceDetails = await rebalanceDetailsResponse.json();
  console.log(`Rebalance execution status: ${rebalanceDetails.data.status}`);
  // status will be one of: "running", "completed", "failed", "canceled", or "terminated"
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "port_abc123",
      "workflowId": "rebal_def456",
      "runId": "run_jkl012",
      "status": "completed",
      "startTime": "2023-05-21T13:30:00.123Z",
      "closeTime": "2023-05-21T13:32:45.678Z",
      "executionTime": "2023-05-21T13:30:00.123Z",
      "taskQueue": "rebalance-queue",
      "result": {
        "transactionHash": "0xTransactionHash789",
        "blockNumber": 12345680,
        "gasUsed": "125000",
        "effectiveGasPrice": "1000000000",
        "status": "success"
      },
      "error": null,
      "historyLength": 32
    },
    "correlationId": "corr_abc123",
    "requestId": "req_xyz789",
    "timestamp": "2023-05-21T13:45:12.345Z"
  }
  ```
</ResponseExample>

## Update Schedule Interval

Updates the rebalance schedule interval for a portfolio.

<ParamField path="portfolioId" type="string" required>
  The unique identifier of the portfolio
</ParamField>

<ParamField body="interval" type="object" required>
  The new interval settings
</ParamField>

<ParamField body="interval.every" type="number" required>
  Interval in milliseconds (min: 1 hour, max: 1 year)
</ParamField>

<ParamField body="interval.offset" type="number" default="0">
  Offset from start time in milliseconds
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url 'https://api.glider.fi/v1/portfolio/port_abc123/update-schedule-interval' \
    --header 'Content-Type: application/json' \
    --header 'X-API-KEY: your_api_key_here' \
    --data '{
      "interval": {
        "every": 43200000,
        "offset": 0
      }
    }'
  ```

  ```javascript JavaScript theme={null}
  const portfolioId = "port_abc123";
  const updateIntervalResponse = await fetch(
    `https://api.glider.fi/v1/portfolio/${portfolioId}/update-schedule-interval`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-API-KEY": "your_api_key_here",
      },
      body: JSON.stringify({
        interval: {
          every: 43200000, // 12 hours in milliseconds
          offset: 0, // Optional: Offset from the start time in milliseconds
        },
      }),
    }
  );

  const updateIntervalResult = await updateIntervalResponse.json();
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "port_abc123",
      "scheduleId": "sched_abc123",
      "previousInterval": {
        "every": 21600000,
        "offset": 0
      },
      "newInterval": {
        "every": 43200000,
        "offset": 0,
        "humanReadable": "12 hours"
      },
      "message": "Portfolio rebalance schedule updated to run every 12 hours",
      "nextScheduledRun": "2023-05-22T01:30:00.000Z"
    },
    "correlationId": "corr_abc123",
    "requestId": "req_xyz789",
    "timestamp": "2023-05-21T13:45:12.345Z"
  }
  ```
</ResponseExample>
