I always seem to struggle to find a concise way of working with custom data via the API. I know that something like...
$updateParams = array('version' => 3, 'contact_id' => $contact_id, 'custom_160' => $badges);
$result=civicrm_api('contact', 'update', $updateParams);
...works for some but not all entities, so I presume that I'm meant to be using the CustomValue API, but whenever I do, my code looks v. clunky / doesn't work / I resort to SQL in frustration. If anyone has a concise example or two of CustomValue in use that would be most appreciated.