Create Spend Policy
client.Billing.SpendPolicies.New(ctx, params) (*SpendPolicyNewResponse, error)
POST/accounts/{account_id}/billing/spend-policies
Creates a spend policy. Each account can have only one enabled usage_block policy per product. A second one returns 409. There is no limit on notification policies.
Security
API Token
The preferred authorization scheme for interacting with the Cloudflare API. Create a token.
Example:
API Email + API Key
The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.
Example:
The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.
Example:
Create Spend Policy
package main
import (
"context"
"fmt"
"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/cloudflare-go/billing"
"github.com/cloudflare/cloudflare-go/option"
)
func main() {
client := cloudflare.NewClient(
option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
)
spendPolicy, err := client.Billing.SpendPolicies.New(context.TODO(), billing.SpendPolicyNewParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
Action: cloudflare.F(billing.SpendPolicyNewParamsActionNotification),
Enabled: cloudflare.F(true),
Name: cloudflare.F("Durable Objects alert"),
ProductID: cloudflare.F(billing.SpendPolicyNewParamsProductIDDurableObjects),
ThresholdCents: cloudflare.F(int64(100)),
NotificationEmail: cloudflare.F("billing@example.com"),
})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v\n", spendPolicy.ID)
}
{
"errors": [
{
"message": "message",
"code": 0
}
],
"messages": [
{
"message": "message",
"code": 0
}
],
"result": {
"id": "6f1c2b8e-3d4a-4f5b-9c6d-7e8f9a0b1c2d",
"account_id": 1234567,
"action": "notification",
"enabled": true,
"name": "Workers cap",
"notification_email": "billing@example.com",
"product_id": "workers",
"threshold_cents": 100,
"version": 1,
"current_state": {
"balance_cents": 0,
"billing_period_end": "2019-12-27T18:11:19.117Z",
"billing_period_start": "2019-12-27T18:11:19.117Z",
"enforcement": {
"applied_generation": 0,
"delivery_status": "pending",
"desired_generation": 0,
"desired_state": "active",
"expires_at": "2019-12-27T18:11:19.117Z"
},
"evaluated_at": "2019-12-27T18:11:19.117Z",
"status": "disabled"
}
},
"success": true
}Returns Examples
{
"errors": [
{
"message": "message",
"code": 0
}
],
"messages": [
{
"message": "message",
"code": 0
}
],
"result": {
"id": "6f1c2b8e-3d4a-4f5b-9c6d-7e8f9a0b1c2d",
"account_id": 1234567,
"action": "notification",
"enabled": true,
"name": "Workers cap",
"notification_email": "billing@example.com",
"product_id": "workers",
"threshold_cents": 100,
"version": 1,
"current_state": {
"balance_cents": 0,
"billing_period_end": "2019-12-27T18:11:19.117Z",
"billing_period_start": "2019-12-27T18:11:19.117Z",
"enforcement": {
"applied_generation": 0,
"delivery_status": "pending",
"desired_generation": 0,
"desired_state": "active",
"expires_at": "2019-12-27T18:11:19.117Z"
},
"evaluated_at": "2019-12-27T18:11:19.117Z",
"status": "disabled"
}
},
"success": true
}