> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wearepion.com/llms.txt
> Use this file to discover all available pages before exploring further.

# API

> This documentation is aimed at developers to define the requirements of technical implementation of the Pion Student Beans code tracking API. More information on the definition of each field and what those fields should be populated with can be found in the knowledge base section.

## \[POST]`/code-tracking/transaction`

`POST https://sb-api.studentbeans.com/code-tracking/transaction`

This endpoint allows you to record the use of a Student Beans voucher code from a transaction.

### **Body Params**

| **Field**                        | **Type**           | **Description**                                                                                                                                                                                                            |
| :------------------------------- | :----------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **brandUid** \[required]         | *string*           | A unique ID attached to your brand issued by Pion. The brandUid is unique per country.                                                                                                                                     |
| **code** \[required]             | *string*           | The code supplied by your brand and issued from a Student Beans platform that is then applied at checkout.                                                                                                                 |
| **transactionValue** \[required] | *float*            | The value of the transaction up to two decimal places as a float. For example, if the transaction value is £10.00, you would enter `10.0` or `10.00` and not `"£10"`, `10` or `"10.0"`.                                    |
| **qualifyingAmount** \[required] | *float*            | The value of the `transactionValue` that is eligible to be discounted, up to two decimal places as a float. For example, if the qualifying amount is £1.50, you would enter `1.5` or `1.50` and not `"£1.50"`, or `"1.5"`. |
| **currency** \[required]         | *string*           | The ISO 4217 currency code for the locale of the transaction.                                                                                                                                                              |
| **timestamp** \[required]        | *integer*          | The timestamp of the transaction between the customer and your brand, in UTC, as Unix Epoch time in seconds.                                                                                                               |
| **newUser**                      | boolean            | Boolean value that determines whether the transaction is a new user.                                                                                                                                                       |
| **productsOrderedSku**           | *array of strings* | An array of string SKU's for the products purchased as part of the transaction.                                                                                                                                            |
| **productsOrderedName**          | *array of strings* | An array of string product names purchased as part of the transaction.                                                                                                                                                     |
| **userAgent**                    | *string*           | The user agent the transaction has been completed from.                                                                                                                                                                    |

Example Request Body:

```
{
  "brandUid": "20406f16-7f0f-11ee-b962-0242ac120002",
  "code": "AFSB20UK-fcf43t",
  "transactionValue": 20.25,
  "qualifyingAmount": 5.25,
  "currency": "GDP",
  "timestamp": 1577836800,
  "newUser": true,
  "productsOrderedSku": ["sk1", "sk2"],
  "productsOrderedName": ["Product Name 1", "Product Name 2"],
  "userAgent": "iOS Safari"
}
```

### **Headers**

|              |                                           |
| :----------- | :---------------------------------------- |
| Content-Type | application-json                          |
| User-Agent   | \<product>/\<product\_version> \<comment> |
| api-key      | \<generated-by-student-beans>             |

### **Example Responses**

##### **200 OK**

```
{
  "data": {
    "type": "transaction",
    "id": "20406f16-7f0f-11ee-b962-0242ac120002",
    "attributes": {
      "brandUid": "20406f16-7f0f-11ee-b962-0242ac120002",
      "code": "AFSB20UK-fcf43t",
      "transactionValue": 20.25,
      "qualifyingAmount": 5.25,
      "currency": "GBP",
      "timestamp": 1577836800,
      "newUser": true,
      "productsOrderedSku": ["sk1", "sk2"],
      "productsOrderedName": ["Product Name 1", "Product Name 2"],
      "userAgent": "iOS Safari"
    }
  }
}
```

##### **400 Bad Request**

```
{
  "errors": [
    {
      "status": 400,
      "title": "Bad Request",
      "detail": "Reason for failed request will be displayed here"
    }
  ]
}
```

### **Implementation Examples**

#### **Curl Example**

```
curl -X POST \
  https://sb-api.studentbeans.com/code-tracking/transaction \
  -H 'Content-Type: application/json' \
  -H 'api-key: <YOUR_API_KEY>' \
  -H 'User-Agent: <YOUR_USER_AGENT>' \
  -d '{
    "brandUid": "20406f16-7f0f-11ee-b962-0242ac120002",
    "code": "AFSB20UK-fcf43t",
    "transactionValue": 20.25,
    "qualifyingAmount": 5.25,
    "currency": "GBP",
    "timestamp": 1577836800
}'
```

#### **Ruby Example**

```
require 'net/http'
require 'uri'
require 'json'

url = 'https://sb-api.studentbeans.com/code-tracking/transaction'
api_key= 'YOUR_API_KEY'
user_agent = 'YOUR_USER_AGENT'

request_body = {
    "brandUid": "20406f16-7f0f-11ee-b962-0242ac120002",
     "code": "AFSB20UK-fcf43t",
     "transactionValue": 20.25,
     "qualifyingAmount": 5.25,
     "currency": "GBP",
     "timestamp": 1577836800
}.to_json

uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Post.new(uri.request_uri)
request["api-key"] = api_key
request["User-Agent"] = user_agent
request['Content-Type'] = 'application/json'
request.body = request_body

response = http.request(request) puts response.body
```

#### **Node Example**

```
const axios = require('axios')

const sbTrackingUrl = 'https://sb-api.studentbeans.com/code-tracking/transaction'
const apiKey = 'YOUR_API_KEY'
const userAgent = 'YOUR_USER_AGENT'

const requestBody = {
  brandUid: '20406f16-7f0f-11ee-b962-0242ac120002',
  code: 'AFSB20UK-fcf43t',
  transactionValue: 20.25,
  qualifyingAmount: 5.25,
  currency: 'GBP',
  timestamp: 1577836800
}

const headers = {
  'api-key': apiKey,
  'Content-Type': 'application/json',
  'User-Agent': userAgent
}

axios
  .post(sbTrackingUrl, requestBody, { headers })
  .then((response) => {
    console.log(response.data)
  })
  .catch((error) => {
    console.error(error)
  })
```

### **Recommended Implementation Architecture**

#### **User Journey & Data Flow**

```mermaid theme={null}
  flowchart TB
    A["User issues a Student Beans<br>Discount Code"] -- Discount code copied and user redirected --> n1["Brand Site"]
    n1 -- User adds products to a basket --> n2["Checkout"]
    n2 --> n3{"Student Beans<br>code used?"}
    n3 -- NO --> n4["Continue brand specific transaction flow"]
    n3 -- YES --> n5["Send Transaction payload to<br>/code-tracking/transaction"]
    n5 --> n6{"Payload is valid?"} & n4
    n6 -- YES --> n7["A 200 response is returned with<br>the created transaction data"]
    n6 -- NO --> n8["A 4XX response is returned<br>highlighting why the payload is invalid"]
```

#### **User Journey & Data Flow with Embedded Connect**

```mermaid theme={null}
flowchart TB
  A["Brand Homepage"] -- User navigates to discount page--> n2["Brand Embedded Connect Page"]
  n2 -- User verifies student status and redeems a Student Beans code --> n3["Brand Site"]
  n3 -- User adds products to a basket --> n4["Checkout"]
  n4 --> n5{"Student Beans<br>code used?"}
  n5 -- NO --> n6["Continue brand specific transaction flow"]
  n5 -- YES --> n7["Send Transaction payload to<br>/code-tracking/transaction"]
  n7 --> n8{"Payload is valid?"} & n6
  n8 -- YES --> n9["A 200 response is returned with<br>the created transaction data"]
  n8 -- NO --> n10["A 4XX response is returned<br>highlighting why the payload is invalid"]
```
