# TIME_ENTRIES

time-entries endpoints.

**Base URL:** `https://api.trustpager.com/functions/v1/api/v1`

## Endpoints

### POST /time-entries

Log a finished stretch of work by hand, after the fact ("I worked 2 hours on the Smith job this morning"). Send started_at and exactly one of ended_at or duration_minutes. Link it to what the work was for (opportunity_id, work_order_id, task_id, contact_id) and put a labour product on it (product_id) so it can be invoiced at that product's price; a work order or task fills in its opportunity when none is sent. The time is logged for the caller unless user_id names someone else, which needs a workspace admin. Refused: an entry longer than 24 hours, a start more than a day in the future, both or neither of ended_at and duration_minutes, and a linked record that is not in this workspace. To time work as it happens, use start_timer instead.

**Scopes:** `time-entries:write` — [full detail](./time-entries/post-time-entries.md)

### DELETE /time-entries/:entry_id

Delete a time entry for good. A person deletes their own time; deleting someone else's needs a workspace admin. Time that is already on an invoice cannot be deleted until it is removed from the invoice. Deleting a running timer discards it without logging any time.

**Scopes:** `time-entries:delete` — [full detail](./time-entries/delete-time-entries-entry-id.md)

### GET /time-entries/running

The timer the caller has running right now, with how long it has run so far (duration_seconds) and what it is linked to. Returns { running: false } when no timer is running. Use it before start_timer or stop_timer when it is not clear whether the clock is on. Free read.

**Scopes:** `time-entries:read` — [full detail](./time-entries/get-time-entries-running.md)

### GET /time-entries/:entry_id

Fetch one time entry by UUID: who did the work, start, end, length, note, whether it is billable or invoiced, and the names of the opportunity, task, contact and labour product it is linked to. Use it to check an entry before changing or deleting it; to find entries, use list_time_entries.

**Scopes:** `time-entries:read` — [full detail](./time-entries/get-time-entries-entry-id.md)

### GET /time-entries/totals

Add up time worked across everything that matches the filters, not just one page: total_seconds, billable_seconds, uninvoiced_billable_seconds (billable time not on an invoice yet) and the number of entries. Takes the same filters as list_time_entries. Use it for "how many hours did Sam work this week", "how much unbilled time is on this opportunity" or "hours per person this month": set group_by to "user", "opportunity" or "day" to also get a row per person, per opportunity or per workspace day. A running timer counts for the time it has run so far. Divide seconds by 3600 for hours. Free read.

**Scopes:** `time-entries:read` — [full detail](./time-entries/get-time-entries-totals.md)

### POST /time-entries/invoice

Make ONE draft invoice from an opportunity's time that is finished, billable and not invoiced yet, then mark that time as invoiced so it is never billed twice. Lines: group_by "person" (default) gives one line per person per labour product, "Labour: <name>" with the dates covered and the hours; "entry" gives one line per entry with its note and date. The price per hour is hourly_rate when sent, otherwise the price of the labour product on each entry; if any time has no price the whole call is refused and names the people whose time needs one. Narrow it with date_to (up to and including that workspace day) or user_id (one person). GST follows the workspace settings and the invoice goes to the opportunity's contact and company. The invoice is a draft: nothing is emailed and nothing is sent to your accounting platform; review it, then send it with send_invoice. When there is no time to bill it returns invoice: null with a message. Running timers are left alone: stop them first.

**Scopes:** `time-entries:write`, `invoices:write` — [full detail](./time-entries/post-time-entries-invoice.md)

### GET /time-entries

List time worked (timesheet entries), newest first. Each entry says who did the work, when it started and ended, how long it was (duration_seconds and duration_hours; a running timer shows the time so far), the note, whether it is billable, whether it has been invoiced, and the opportunity, work order, task, contact and labour product it is linked to, with their names. Filter by person (user_id, or "me"), opportunity, work order, task, contact, a range of workspace days (date_from, date_to), billable, invoiced and running. Returns one page: for how many hours in total, use get_time_totals with the same filters. Free read.

**Scopes:** `time-entries:read` — [full detail](./time-entries/get-time-entries.md)

### POST /time-entries/start

Start a timer for the person making the call, from now. Optionally say what it is for (note, opportunity_id, work_order_id, task_id, contact_id) and which labour product prices it (product_id). A person has one timer at a time: if one is already running this call is refused with a conflict that says since when and gives the running entry's id, unless stop_running is true, which stops the running timer and starts this one. Stop it with stop_timer. A timer can only be started for the caller, never for someone else.

**Scopes:** `time-entries:write` — [full detail](./time-entries/post-time-entries-start.md)

### POST /time-entries/stop

Stop the caller's running timer and return the finished entry with its length. It ends now, unless ended_at gives the earlier moment the work really stopped ("I actually stopped at 4:30"), which must be after the timer started and not in the future. note replaces the note the timer was started with. Answers "not found" when the caller has no timer running: check with get_running_timer.

**Scopes:** `time-entries:write` — [full detail](./time-entries/post-time-entries-stop.md)

### PATCH /time-entries/:entry_id

Change a time entry: its start, its end or length, its note, whether it is billable, and what it is linked to (send an empty string to clear a link). Send only the fields to change. On a finished entry the length is worked out again when the times change. A person changes their own time; changing someone else's needs a workspace admin. Time that is already on an invoice is locked: only its note can change, and anything else is refused until it is removed from the invoice. Who did the work cannot be changed: delete the entry and log it again.

**Scopes:** `time-entries:write` — [full detail](./time-entries/patch-time-entries-entry-id.md)
