Skip to main content

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​

  1. Flag the customer. Send a POST request to /v1/customers/{id}/flag with a reason of 1 to 500 characters. The response holds the customer, and its flag is an object with the reason and created, the time of the flag in Unix seconds. Any other field is refused with 422 naming it. An unknown or deleted customer answers 404.

  2. List the flagged customers. Send a GET request to /v1/customers?flagged=true. The list holds only flagged customers. flagged=false holds only the others, and both combine with the other filters of the list.

  3. Read the event. Black records customer.flagged, with the reason in data.reason and {"flag":null} in data.previous_attributes. List it with GET /v1/events?type=customer.flagged&customer={id}. Black records no customer.updated for a flag.

  4. Remove the flag when the review is over. Send a POST request to /v1/customers/{id}/unflag with a reason of 1 to 500 characters. The response holds the customer, and its flag is null. Black records customer.unflagged, with the reason in data.reason and the flag the customer had in data.previous_attributes.flag.

  5. 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, send unflag with the reason you are done with, then flag the customer again.
  • customer_not_flagged (409). You sent unflag for a customer that has no flag, and Black changes nothing. Read the customer: its flag is null, so there is nothing to remove.
  • parameter_invalid (422) naming reason. 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:

  • object is activity_entry, and id starts with act_.
  • created is the time of the change in Unix seconds, and livemode is the mode of the environment the change belongs to. It is null for a change of the account as a whole.
  • action names what happened, such as customer.flagged, settings.changed, invoice.marked_paid, subscription.canceled or product.updated.
  • actor holds a type, one of api_client, vendor_client, operator, system, team_member, vendor_team_member, checkout_session, customer and app, and an id. The id is null for operator and system, and for the others the ID of the client, person, checkout session, customer or app that acted. See Events for what each type is.
  • subject is the ID of the object the entry is about, and customer is the customer's ID when the change concerns one, and null otherwise.
  • reason is the reason the change gave, when it gave one.
  • event is the ID of the event the change recorded, and null when it recorded none.
  • request_id and ip_address are those of the request that made the change. Both are null for a change made by scheduled work or by Black's operators.
  • changes holds each field the change altered as an object of old and new values, keyed by the field's name. A secret appears only as {"secret_changed":true}, never with its value. A flag records flag_reason and flagged_at, and a feature's metadata change records one key at a time, as in metadata.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:

  • action is an action name, or a prefix with a trailing .* such as customer.*. A prefix that matches no action, and any other value, is refused with 422 naming action.
  • subject is the ID of the object the entry is about, such as prod_… or feat_…. An ID that names nothing lists nothing.
  • customer is a customer's ID.
  • actor_type is one of the nine types of actor above. actor is the ID of one actor, and it is accepted only with actor_type api_client, vendor_client, team_member, vendor_team_member or customer; otherwise it is refused with 422 naming actor.
  • created_from includes the moment it names, and created_until excludes 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​