> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.afternoon.co/llms.txt.
> For full documentation content, see https://docs.afternoon.co/llms-full.txt.

# Cancel a subscription

POST https://api.afternoon.co/v1/subscriptions/{id}/cancel
Content-Type: application/json

Cancels a subscription immediately or at the end of the current billing period.

Reference: https://docs.afternoon.co/api-reference/afternoon-events-api/subscriptions/cancel

## OpenAPI Specification

```yaml
openapi: 3.1.0
info:
  title: Afternoon Events API
  version: 1.0.0
paths:
  /v1/subscriptions/{id}/cancel:
    post:
      operationId: cancel
      summary: Cancel a subscription
      description: >-
        Cancels a subscription immediately or at the end of the current billing
        period.
      tags:
        - subpackage_subscriptions
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
            format: cuid
        - name: Authorization
          in: header
          description: API key obtained from the Afternoon dashboard
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Subscription canceled
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SubscriptionResponse'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          description: Subscription not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CancelSubscriptionRequest'
servers:
  - url: https://api.afternoon.co
  - url: http://localhost:8787
components:
  schemas:
    CancelSubscriptionRequestCancelMode:
      type: string
      enum:
        - now
        - end_of_period
      description: >-
        When to cancel: "now" cancels immediately, "end_of_period" cancels at
        the end of the current billing cycle
      title: CancelSubscriptionRequestCancelMode
    CancelSubscriptionRequestCancelNowPrepaidRefundPolicy:
      type: string
      enum:
        - refund_prorated_prepaid_fixed_fees
        - no_refund_prepaid_fixed_fees
      description: >-
        Applies only when cancel_mode is "now": refund prorated prepaid fixed
        fees or skip refunds.
      title: CancelSubscriptionRequestCancelNowPrepaidRefundPolicy
    CancelSubscriptionRequest:
      type: object
      properties:
        cancel_mode:
          $ref: '#/components/schemas/CancelSubscriptionRequestCancelMode'
          description: >-
            When to cancel: "now" cancels immediately, "end_of_period" cancels
            at the end of the current billing cycle
        cancel_now_prepaid_refund_policy:
          $ref: >-
            #/components/schemas/CancelSubscriptionRequestCancelNowPrepaidRefundPolicy
          description: >-
            Applies only when cancel_mode is "now": refund prorated prepaid
            fixed fees or skip refunds.
      required:
        - cancel_mode
      title: CancelSubscriptionRequest
    SubscriptionStatus:
      type: string
      enum:
        - pending_payment_method
        - trialing
        - active
        - past_due
        - canceled
      title: SubscriptionStatus
    Subscription:
      type: object
      properties:
        id:
          type: string
        customer_id:
          type: string
        plan_id:
          type: string
        plan_code:
          type:
            - string
            - 'null'
          description: Human-readable plan code
        status:
          $ref: '#/components/schemas/SubscriptionStatus'
        start_date:
          type: string
          description: ISO 8601 subscription start date
        billing_anchor_day:
          type: integer
          description: Day of month for billing anchor
        next_renewal_date:
          type:
            - string
            - 'null'
          description: ISO 8601 next renewal date
        cancel_at_period_end:
          type: boolean
          description: >-
            True when cancellation is scheduled for the end of the current
            billable period
        cancel_at:
          type:
            - string
            - 'null'
          description: ISO 8601 scheduled cancellation timestamp
        plan_transition_at_period_end:
          type: boolean
          description: >-
            True when a plan transition is scheduled for end of current billable
            period
        plan_transition_at:
          type:
            - string
            - 'null'
          description: ISO 8601 scheduled plan transition timestamp
        plan_transition_to_plan_id:
          type:
            - string
            - 'null'
          description: Target plan ID for a scheduled transition
        canceled_at:
          type:
            - string
            - 'null'
          description: >-
            ISO 8601 actual cancellation timestamp (set when status transitions
            to canceled)
        created_at:
          type: string
          description: ISO 8601 creation timestamp
        updated_at:
          type: string
          description: ISO 8601 last-updated timestamp
      required:
        - id
        - customer_id
        - plan_id
        - plan_code
        - status
        - start_date
        - billing_anchor_day
        - next_renewal_date
        - cancel_at_period_end
        - cancel_at
        - plan_transition_at_period_end
        - plan_transition_at
        - plan_transition_to_plan_id
        - canceled_at
        - created_at
        - updated_at
      title: Subscription
    SubscriptionResponse:
      type: object
      properties:
        success:
          type: boolean
        subscription:
          $ref: '#/components/schemas/Subscription'
        request_id:
          type: string
      required:
        - success
        - subscription
        - request_id
      title: SubscriptionResponse
    ErrorResponseError:
      type: object
      properties:
        code:
          type: string
        message:
          type: string
        details:
          oneOf:
            - description: Any type
            - type: 'null'
      required:
        - code
        - message
      title: ErrorResponseError
    ErrorResponse:
      type: object
      properties:
        success:
          type: boolean
        error:
          $ref: '#/components/schemas/ErrorResponseError'
        request_id:
          type: string
          description: Request ID for tracing
      required:
        - success
        - error
      title: ErrorResponse
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: API key obtained from the Afternoon dashboard

```

## SDK Code Examples

```typescript
import { AfternoonClient } from "afternoon-sdk";

async function main() {
    const client = new AfternoonClient({
        token: "YOUR_TOKEN_HERE",
    });
    await client.subscriptions.cancel("cm5x9z8nv0000h85r7l9p2k1m", {
        cancelMode: "now",
    });
}
main();

```

```python
import requests

url = "https://api.afternoon.co/v1/subscriptions/cm5x9z8nv0000h85r7l9p2k1m/cancel"

payload = { "cancel_mode": "now" }
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.afternoon.co/v1/subscriptions/cm5x9z8nv0000h85r7l9p2k1m/cancel"

	payload := strings.NewReader("{\n  \"cancel_mode\": \"now\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Bearer <token>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.afternoon.co/v1/subscriptions/cm5x9z8nv0000h85r7l9p2k1m/cancel")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"cancel_mode\": \"now\"\n}"

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.afternoon.co/v1/subscriptions/cm5x9z8nv0000h85r7l9p2k1m/cancel")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"cancel_mode\": \"now\"\n}")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.afternoon.co/v1/subscriptions/cm5x9z8nv0000h85r7l9p2k1m/cancel', [
  'body' => '{
  "cancel_mode": "now"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://api.afternoon.co/v1/subscriptions/cm5x9z8nv0000h85r7l9p2k1m/cancel");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"cancel_mode\": \"now\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = ["cancel_mode": "now"] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.afternoon.co/v1/subscriptions/cm5x9z8nv0000h85r7l9p2k1m/cancel")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```