# GIFTS

gifts endpoints.

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

## Endpoints

### POST /gifts/:gift_id/pdf-pages

Add one of your own finished PDFs (from Files) to every person's gift PDF, at the start or the end, as it is. Up to 10 PDFs and 40 pages.

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-pdf-pages.md)

### POST /gifts/:gift_id/questions

Add a question the gift asks on the person's page: single (One answer), multi (Several answers), text (Short text), long (Long text, a paragraph box), website (Their website), number (A number, money or a percentage, with min, max, step and format) or file (A file or photo; shown, but not answerable on the page yet). Every question has a key calculated results use (made from the question when you leave it blank). No points or right answers: every answer goes into what the gift makes. allow_other adds a "Something else" box; required false lets them skip it.

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-questions.md)

### POST /gifts/:gift_id/script/approve-code

Approve the gift script's current code so it makes real gifts (publishes it as part of approving). Admin only. Until approved, people see nothing made by the script, and cold campaigns giving this gift cannot start.

**Scopes:** `admin` — [full detail](./gifts/post-gifts-gift-id-script-approve-code.md)

### POST /gifts/:gift_id/archive

Archive a gift. People already given it keep their gift; a Lead Magnet still giving it keeps making it until you pick another gift on its Made for you block.

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-archive.md)

### POST /gifts/:gift_id/calculate

Work out the gift's calculated results (its calculations) for sample answers, instantly: { calculated: { outputs: [{ key, label, value, display, format }] } } with display written the way a person reads it ($3,597.30, 80%). Answers go by question key or id; a choice can be the option's words. Nothing is saved or charged. Send calculations to try a definition that is not saved yet; its problems come back in plain words.

**Scopes:** `gifts:read` — [full detail](./gifts/post-gifts-gift-id-calculate.md)

### POST /gifts

Create a gift as a draft. Then add its questions (add_gift_question or generate_gift_questions from its brief), say what it makes (makes), set your PDF design (get_gift_starter_design, set_gift_pdf_design), try it (preview_gift), set it live with update_gift, and put it on a Lead Magnet's Made for you block (gift_id).

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

### DELETE /gifts/:gift_id

Delete a gift nobody has been given and no Lead Magnet uses. Refused once anyone got it (archive it instead).

**Scopes:** `gifts:delete` — [full detail](./gifts/delete-gifts-gift-id.md)

### DELETE /gifts/:gift_id/questions/:question_id

Remove a question from a gift. Answers already given stay on their gift.

**Scopes:** `gifts:write` — [full detail](./gifts/delete-gifts-gift-id-questions-question-id.md)

### DELETE /gifts/:gift_id/script/secrets/:secret_name

Remove a third-party secret from a gift's script.

**Scopes:** `gifts:write`, `automations:write` — [full detail](./gifts/delete-gifts-gift-id-script-secrets-secret-name.md)

### POST /gifts/:gift_id/duplicate

Copy a gift as a new draft called "<name> (copy)": its brief, settings, questions, what it makes, research, PDF design and own PDF pages. The people it was made for, their gifts and the Lead Magnets it is on are not copied, and neither is a gift script (write it again with generate_gift_script). Returns the new gift's id (copied_from is the original).

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-duplicate.md)

### POST /gifts/:gift_id/runs/:run_id/email

Email someone their made gift (the PDF attached), in your workspace's email design, whatever the gift's email setting says.

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-runs-run-id-email.md)

### POST /gifts/:gift_id/questions/generate

Write the gift's questions with AI from a brief (defaults to the gift's own brief), and add them. Costs 16 credits a go, refunded when nothing usable comes back. replace=true swaps out the current questions.

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-questions-generate.md)

### POST /gifts/:gift_id/script/generate

Write (or rewrite) the Custom Script that makes a gift, from a plain-English description of the gift. The script gets the person (reader: first_name, business_name, industry, city, state, postcode, website, why_now), their answers to the gift's questions (answers) and the quiz score when there is one (quiz), and returns { title, text, html, url, image_url }. New code needs approval (approve_gift_script) before it makes real gifts; try it first with test_gift_script.

**Scopes:** `gifts:write`, `automations:write` — [full detail](./gifts/post-gifts-gift-id-script-generate.md)

### GET /gifts/:gift_id

Get one gift: its questions (each with its key, and a number question's min, max, step and format), its calculated results (calculations: rate tables and formulas), what it makes (makes) and its one price (price_credits: the most one made gift costs for its fixed parts, research lookups included; pdf_credits_per_page: added for each page of the PDF made, null when it makes no PDF; price in words with each part), the research settings, its PDF design and your own PDF pages, how people get it, the Lead Magnets that give it, its stats, and for a script gift whether the script can run.

**Scopes:** `gifts:read` — [full detail](./gifts/get-gifts-gift-id.md)

### GET /gifts/:gift_id/runs/:run_id

Get one person's gift: what they told us (their answers in words), their quiz score when the gift used it, what we found (research: facts, each with its numbered source, the lookups made and their credits), and every item with its status, its stage while it is made (filming, rendering, chatgpt, picture) and its content when ready: the PDF as a download link that works for an hour; picture and badge image_url; video video_url and poster_url; audit score and checks; website preview_url and image_url. Works for a preview run too.

**Scopes:** `gifts:read` — [full detail](./gifts/get-gifts-gift-id-runs-run-id.md)

### GET /gifts/:gift_id/script

Get the Custom Script that makes a gift (when the gift makes a script's output): id, description, code, approval and publish state (live_ready, live_block_reason), secret names, last test result. 404 until the script is first written.

**Scopes:** `gifts:read` — [full detail](./gifts/get-gifts-gift-id-script.md)

### GET /gifts/:gift_id/pdf-design/starter

Get the starter PDF design: a designed A4 page (HTML) with every value in place and instructions for your own AI at the top, plus the list of values a design can use ({{reader.first_name}}, {{plan.html}}, {{quiz.score}} and the rest) and the gift's current design.

**Scopes:** `gifts:read` — [full detail](./gifts/get-gifts-gift-id-pdf-design-starter.md)

### GET /gifts/:gift_id/runs

List the people given a gift, newest first: who they are, where it came from (the Lead Magnet, and the cold campaign for a lead), its status (waiting, queued, researching, making, ready, failed; waiting = it waits for their answers on their page; researching = looking things up first), each item's status (pdf, plan, image, video, audit, website, script), credits charged, when it was made and emailed. previews=true lists the samples made with preview_gift instead. Filters: status, lead_magnet_id, contact_id, previews, limit, offset.

**Scopes:** `gifts:read` — [full detail](./gifts/get-gifts-gift-id-runs.md)

### GET /gifts

List the workspace's gifts, newest change first, archived ones left out unless status=archived: what each makes and its price, the Lead Magnets that give it (used_on), and stats (got_it, made, made_percent, waiting for answers, failed). Filters: status, makes (pdf, image, video, audit, website, script), search, limit, offset. price_credits is each gift's one price for its fixed parts (research included) and pdf_credits_per_page is added for each page of a made PDF (null when it makes no PDF); previews are not counted in stats.

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

### POST /gifts/:gift_id/preview

Make the gift for a sample business (and sample answers or a quiz score), to see what people will get. A gift of a plan, a PDF and a website design picture comes back now (queued false, items: the PDF as a link that works for a day). A gift with research, a made picture, a badge, a video, an audit or a rebuilt website is made in the background (queued true, run_id): follow it with get_gift_run, whose items carry the links (picture, badge, video, PDF, preview) when ready. Charged like a real gift: each part's price is held when it starts and what it does not use is given back (a part that fails keeps only what was already spent); nothing is saved on a person or emailed.

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-preview.md)

### POST /gifts/:gift_id/runs/:run_id/remake

Make one person's gift again from the start with the gift's current settings (after a failure, or after you changed the design). Charged again as it is made, and emailed again when the gift emails it.

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-runs-run-id-remake.md)

### DELETE /gifts/:gift_id/pdf-pages/:page_id

Take one of your own PDFs out of the gift's PDF (page_id from get_gift pdf_extra_pages).

**Scopes:** `gifts:write` — [full detail](./gifts/delete-gifts-gift-id-pdf-pages-page-id.md)

### POST /gifts/:gift_id/questions/reorder

Put a gift's questions in a new order: send every question id once, in order.

**Scopes:** `gifts:write` — [full detail](./gifts/post-gifts-gift-id-questions-reorder.md)

### PATCH /gifts/:gift_id/script/code

Save hand-written code for a gift's script, exactly as given (it must export async function runScript(ctx, tools) and return { title, text, html, url, image_url }). Saved code needs approval again before it makes real gifts.

**Scopes:** `gifts:write`, `automations:write` — [full detail](./gifts/patch-gifts-gift-id-script-code.md)

### PUT /gifts/:gift_id/pdf-design

Set the gift's PDF design: html (one page, from get_gift_starter_design), or file_id of a zip in Files holding the page with its images, fonts and stylesheets (packed into the page), or clear: true for the starter design. Scripts are removed. warnings lists values we do not fill.

**Scopes:** `gifts:write` — [full detail](./gifts/put-gifts-gift-id-pdf-design.md)

### PUT /gifts/:gift_id/script/secrets/:secret_name

Add or replace a third-party secret the gift script needs (write-only, the value is never returned). The script reads it as secrets.NAME.

**Scopes:** `gifts:write`, `automations:write` — [full detail](./gifts/put-gifts-gift-id-script-secrets-secret-name.md)

### POST /gifts/:gift_id/script/test

Try the gift script on a sample person (or a reader you describe) without approving it. Runs in the background: read the outcome with get_gift_script (last_test_status, last_test_result) a few seconds later. Charged like one Custom Script run.

**Scopes:** `gifts:write`, `automations-trigger:trigger` — [full detail](./gifts/post-gifts-gift-id-script-test.md)

### PATCH /gifts/:gift_id

Change a gift: name, status, brief, what it makes (makes merges per maker), research settings, calculated results (calculations replaces the whole definition), and how people get it (on their page, by email, made again when they change their answers). A live gift must make something or have calculated results.

**Scopes:** `gifts:write` — [full detail](./gifts/patch-gifts-gift-id.md)

### PATCH /gifts/:gift_id/questions/:question_id

Change a gift question: send only what changes.

**Scopes:** `gifts:write` — [full detail](./gifts/patch-gifts-gift-id-questions-question-id.md)
