# Programmable Spend-based CUD Purchase Did you know it’s possible to [purchase spend-based CUDs programmatically](https://cloud.google.com/marketplace/docs/commitment-api-purchasing), including (for now) the legacy Autopilot CUD and the new Compute flexible CUD? Let’s take it for a spin. Firstly, you’ll need the [procurement API](https://console.developers.google.com/apis/api/cloudcommerceconsumerprocurement.googleapis.com/overview) enabled and IAM permissions setup. Follow the steps in [Before you begin](https://cloud.google.com/marketplace/docs/commitment-api-purchasing#before_you_begin) in the official doc. The **`gcloud`** CLI will need to be authenticated in order to generate the needed access token (`gcloud auth print-access-token` should print out an auth token) Define your values. The offer name can be found in the [docs](https://cloud.google.com/marketplace/docs/commitment-api-purchasing#identify_the_offer_you_want_to_purchase). Note there are two offer names, one if you’ve yet to opt-in to the new CUD model and one if you have. ``` BILLING_ACCOUNT_ID=00E8D7-6F2429-92711B DISPLAY_NAME=test-cud-oct15 REGION=us-central1 HOURLY_COMMIT=0.01 PROJECT_ID=gke-autopilot-test # 3 year CUD OFFER_NAME=services/container.googleapis.com/standardOffers/fcf378c1-fbe0-4aaa-b05e-9597f8b45578 ``` Warning: Be careful to verify all values, especially region and “HOURLY\_COMMIT” as CUDs are not easy to cancel (if you do run into a problem, be sure to get in touch with cloud billing within 14 days!). The commitment amount is the post-discounted value (i.e. the actual amount that you pay). Here’s my CURL command ``` curl "https://cloudcommerceconsumerprocurement.googleapis.com/v1alpha1/billingAccounts/$BILLING_ACCOUNT_ID/orders:place" \ --header 'Content-Type: application/json' \ --header "Authorization: Bearer $(gcloud auth print-access-token)" \ --header "X-Goog-User-Project: $PROJECT_ID" \ -d '@-' <<EOF { "displayName": "$DISPLAY_NAME", "lineItemInfo": [{ "parameters": [{ "name": "hourly_commit", "value": { "doubleValue": $HOURLY_COMMIT } }, { "name": "region", "value": { "stringValue": "$REGION" } }], "offer": "$OFFER_NAME" }] } EOF ``` If you get a precondition failed, try the other offer code (opt-in or not opted-in). ``` { "error": { "code": 400, "message": "Precondition check failed.", "status": "FAILED_PRECONDITION" } } ``` When it works, you get back the order ID like so: ``` { "name": "billingAccounts/00E8D7-6F2429-92711B/orders/1d8392d6-10c9-423d-b659-15d6075298c5/operations/PLACE_ORDER-5933304c-3a5a-4db9-8c90-9d39afa3ebcc" } ``` This is not an instantaneous request (though it’s pretty quick taking seconds). To check the status, refresh the CUD UI, or use the returned offer name to query the status like so. ``` OPERATION_NAME=billingAccounts/00E8D7-6F2429-92711B/orders/1d8392d6-10c9-423d-b659-15d6075298c5/operations/PLACE_ORDER-5933304c-3a5a-4db9-8c90-9d39afa3ebcc curl "https://cloudcommerceconsumerprocurement.googleapis.com/v1alpha1/$OPERATION_NAME" \ --header 'Content-Type: application/json' \ --header "Authorization: Bearer $(gcloud auth print-access-token)" \ --header "X-Goog-User-Project: $PROJECT_ID" ```