Flag a customer and read the activity log
What you will build
You flag a customer for review with a reason, list the flagged customers, and remove the flag. You then read the activity log to see who made each change, when, and with which values. You also learn what each refusal means and what a test client and a vendor see in the log.
Before you start
You need a test environment, an access token and a customer. See Authentication. The sample creates the customer.
A flag marks a customer for your own review. It changes no subscription, no invoice, no benefit and no billing step: the lookup of what the customer holds answers as before. Act on the flag in your own system.
Steps
-
Flag the customer. Send a
POSTrequest to/v1/customers/{id}/flagwith areasonof 1 to 500 characters. The response holds the customer, and itsflagis an object with thereasonandcreated, the time of the flag in Unix seconds. Any other field is refused with422naming it. An unknown or deleted customer answers404. -
List the flagged customers. Send a
GETrequest to/v1/customers?flagged=true. The list holds only flagged customers.flagged=falseholds only the others, and both combine with the other filters of the list. -
Read the event. Black records
customer.flagged, with the reason indata.reasonand{"flag":null}indata.previous_attributes. List it withGET /v1/events?type=customer.flagged&customer={id}. Black records nocustomer.updatedfor a flag. -
Remove the flag when the review is over. Send a
POSTrequest to/v1/customers/{id}/unflagwith areasonof 1 to 500 characters. The response holds the customer, and itsflagisnull. Black recordscustomer.unflagged, with the reason indata.reasonand the flag the customer had indata.previous_attributes.flag. -
Give a new reason by removing the flag, then flagging the customer again. Black keeps only the current flag on the customer. The history is in the events and in the activity log.
A request that creates or updates a customer with a flag field is refused with 422 naming flag. Use the two endpoints above.
Handle a refusal
Each refusal gives an error with a code. See Errors for every code.
customer_already_flagged(409). The customer is already flagged, and Black changes nothing. To give another reason, sendunflagwith the reason you are done with, then flag the customer again.customer_not_flagged(409). You sentunflagfor a customer that has no flag, and Black changes nothing. Read the customer: itsflagisnull, so there is nothing to remove.parameter_invalid(422) namingreason. The reason is missing, empty or longer than 500 characters. Send a reason of 1 to 500 characters.
Read the activity log
The activity log records who changed what, with the values before and after. It is separate from the events: it records changes that send no event too, and Black never delivers it to an endpoint. An entry belongs to your account. Black writes it in the same transaction as the change, so a refused request leaves none.
Send a GET request to /v1/activity-entries for the list, newest first, and to /v1/activity-entries/{id} for one entry. Each entry holds:
objectisactivity_entry, andidstarts withact_.createdis the time of the change in Unix seconds, andlivemodeis the mode of the environment the change belongs to. It isnullfor a change of the account as a whole.actionnames what happened, such ascustomer.flagged,settings.changed,invoice.marked_paid,subscription.canceledorproduct.updated.actorholds atype, one ofapi_client,vendor_client,operator,system,team_member,vendor_team_member,checkout_session,customerandapp, and anid. Theidisnullforoperatorandsystem, and for the others the ID of the client, person, checkout session, customer or app that acted. See Events for what each type is.subjectis the ID of the object the entry is about, andcustomeris the customer's ID when the change concerns one, andnullotherwise.reasonis the reason the change gave, when it gave one.eventis the ID of the event the change recorded, andnullwhen it recorded none.request_idandip_addressare those of the request that made the change. Both arenullfor a change made by scheduled work or by Black's operators.changesholds each field the change altered as an object ofoldandnewvalues, keyed by the field's name. A secret appears only as{"secret_changed":true}, never with its value. A flag recordsflag_reasonandflagged_at, and a feature's metadata change records one key at a time, as inmetadata.discount_value.
Black shows no internal key of the account or the environment, and no operator's name.
Filter the log
Every filter is optional, and filters combine:
actionis an action name, or a prefix with a trailing.*such ascustomer.*. A prefix that matches no action, and any other value, is refused with422namingaction.subjectis the ID of the object the entry is about, such asprod_…orfeat_…. An ID that names nothing lists nothing.customeris a customer's ID.actor_typeis one of the nine types ofactorabove.actoris the ID of one actor, and it is accepted only withactor_typeapi_client,vendor_client,team_member,vendor_team_memberorcustomer; otherwise it is refused with422namingactor.created_fromincludes the moment it names, andcreated_untilexcludes it. Black reads both in UTC.
A filter that you send as a list, such as action[]=customer.flagged, is refused with 422 naming it. The list takes limit, starting_after and ending_before as every list does, and a cursor must name an entry you may read, or the answer is 404 naming the parameter.
Know what you may read
A test client reads the test environment's entries and the entries of your account that belong to no environment. It never reads a live entry: a live entry is in no list and answers 404 by its ID. A live client reads the live entries in the same way.
A vendor's client reads a sub-account's log by naming it in Black-Account. Without the header, the request is refused with 403 and the code account_required. Add the header with the sub-account's ID.
Nobody changes or deletes an entry. A PATCH or DELETE request to an entry answers 405 with the code invalid_request, and the header Allow lists GET and HEAD. A live entry stays for good: Black sets no retention period.
The requests below are refused: a vendor's client reads the log without Black-Account, and a client of an account deletes an entry. The sample writes PLACEHOLDER_VENDOR_ACCESS_TOKEN where the vendor's access token goes, and PLACEHOLDER_ACCESS_TOKEN for a client of an account:
curl --silent https://api.black.example/v1/activity-entries \
--header "Authorization: Bearer PLACEHOLDER_VENDOR_ACCESS_TOKEN"
curl --silent --request DELETE https://api.black.example/v1/activity-entries/act_unknown \
--header "Authorization: Bearer PLACEHOLDER_ACCESS_TOKEN"
A runnable sample
The sample creates a customer with a reference, flags the customer, lists the flagged customers and the event, reads the activity log by customer, by action and by the ID of the entry, and removes the flag. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes, reads each ID from the response before it, and needs sed. A test environment takes five writes a second, so the sample waits a second after each write. The flagging twice and the removal twice are the two refusals, which answer 409.
TOKEN=PLACEHOLDER_ACCESS_TOKEN
CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"name":"Lina","email":"lina@example.com","external_reference":"flag-guest-5107"}' \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')
curl --silent --request POST https://api.black.example/v1/customers/$CUSTOMER/flag \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"reason":"Many bookings cancelled in one day"}'
curl --silent --request POST https://api.black.example/v1/customers/$CUSTOMER/flag \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"reason":"Many bookings cancelled in one day"}'
curl --silent "https://api.black.example/v1/customers?flagged=true" \
--header "Authorization: Bearer $TOKEN"
curl --silent "https://api.black.example/v1/events?type=customer.flagged&customer=$CUSTOMER" \
--header "Authorization: Bearer $TOKEN"
curl --silent "https://api.black.example/v1/activity-entries?customer=$CUSTOMER" \
--header "Authorization: Bearer $TOKEN"
ENTRY=$(curl --silent "https://api.black.example/v1/activity-entries?customer=$CUSTOMER&action=customer.*&limit=1" \
--header "Authorization: Bearer $TOKEN" \
| sed -n 's/.*"data":\[{"object":"activity_entry","id":"\(act_[0-9a-z]*\)".*/\1/p')
curl --silent https://api.black.example/v1/activity-entries/$ENTRY \
--header "Authorization: Bearer $TOKEN"
curl --silent --request POST https://api.black.example/v1/customers/$CUSTOMER/unflag \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"reason":"Checked with the hotel"}'
curl --silent --request POST https://api.black.example/v1/customers/$CUSTOMER/unflag \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"reason":"Checked with the hotel"}'
curl --silent "https://api.black.example/v1/events?type=customer.unflagged&customer=$CUSTOMER" \
--header "Authorization: Bearer $TOKEN"
curl --silent "https://api.black.example/v1/activity-entries?subject=$CUSTOMER&action=customer.*" \
--header "Authorization: Bearer $TOKEN"
The create answers 201, and the first flag, the lists and the reads answer 200. The second flag answers 409 with customer_already_flagged, and the second removal answers 409 with customer_not_flagged. The customer's flag holds the reason and created after the first flag, and the list with flagged=true holds the customer. The first activity read holds the entry customer.flagged with the reason, your client as the actor and the ID of the event, and the read of one entry by its ID answers the same entry as the list. The removal answers the customer with flag null, and the last read holds the removal's entry before the flag's. To run the sample again in the same environment, change flag-guest-5107: a reference that a customer already holds answers that customer, and the customer is then already flagged or not flagged only as the last run left it.
What to do next
- Read what the lookup of a customer holds, and see that a flag does not change it, in Check what a customer holds.
- Read how a vendor acts for a sub-account in Create and act for sub-accounts.
- Read every event, including
customer.flaggedandcustomer.unflagged, in Events. - Read every error code in Errors.