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

# Cancel Subscription

> Cancel an active subscription

<Note>
  This endpoint cancels a subscription. You can choose to cancel immediately or at the end of the current billing period.
</Note>

## Authentication

This endpoint requires a valid API key with `subscriptions:write` permissions.

```bash theme={null}
Authorization: Bearer YOUR_API_KEY
```

## Path Parameters

<ParamField path="id" type="string" required>
  The unique identifier of the subscription to cancel
</ParamField>

## Request Body

<ParamField body="cancel_at_period_end" type="boolean">
  If true, cancel at end of current period. If false, cancel immediately (default: true)
</ParamField>

<ParamField body="reason" type="string">
  Cancellation reason: "too\_expensive", "missing\_features", "not\_using", "switching\_provider", "other"
</ParamField>

<ParamField body="feedback" type="string">
  Additional feedback about cancellation
</ParamField>

<ParamField body="cancel_immediately" type="boolean">
  Override to cancel immediately regardless of period
</ParamField>

### Response

Returns the cancelled subscription object.

<RequestExample>
  ```bash cURL theme={null}
  curl --location --request POST 'https://api.stateset.com/v1/subscriptions/sub_0901f083-aa1c-43c5-af5c-0a9d2fc64e30/cancel' \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --data-raw '{
      "cancel_at_period_end": true,
      "reason": "too_expensive",
      "feedback": "The pricing increased beyond our budget"
  }'
  ```

  ```javascript Node.js theme={null}
  const subscription = await stateset.subscriptions.cancel(
    'sub_0901f083-aa1c-43c5-af5c-0a9d2fc64e30',
    {
      cancel_at_period_end: true,
      reason: "too_expensive",
      feedback: "The pricing increased beyond our budget"
    }
  );
  ```

  ```python Python theme={null}
  subscription = stateset.subscriptions.cancel(
    'sub_0901f083-aa1c-43c5-af5c-0a9d2fc64e30',
    cancel_at_period_end=True,
    reason="too_expensive",
    feedback="The pricing increased beyond our budget"
  )
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
    "id": "sub_0901f083-aa1c-43c5-af5c-0a9d2fc64e30",
    "object": "subscription",
    "status": "active",
    "cancel_at_period_end": true,
    "cancel_at": "2024-03-01T00:00:00Z",
    "cancelled_at": "2024-01-20T15:00:00Z",
    "cancellation_details": {
      "reason": "too_expensive",
      "feedback": "The pricing increased beyond our budget",
      "cancelled_by": "user_123"
    },
    "current_period_end": "2024-03-01",
    "ended_at": null,
    "customer_id": "cust_abc123",
    "plan": {
      "id": "plan_monthly_pro",
      "name": "Professional Monthly"
    },
    "metadata": {
      "cancellation_flow": "customer_portal",
      "retention_offered": true,
      "retention_accepted": false
    }
  }
  ```
</ResponseExample>
