# Create Order

POST https://global-api-sandbox.afterpay.com/v2/orders
Content-Type: application/json

This is the first step in the on file grant purchase process. Use the token in the response to initiate the payment


Reference: https://developers.cash.app/cash-app-afterpay/api-reference/reference/orders/create-grant

## OpenAPI Specification

```yaml
openapi: 3.1.1
info:
  title: Create Order
  version: endpoint_.create-grant
paths:
  /v2/orders:
    post:
      operationId: create-grant
      summary: Create Order
      description: >
        This is the first step in the on file grant purchase process. Use the
        token in the response to initiate the payment
      tags:
        - []
      parameters:
        - name: Authorization
          in: header
          description: Basic authentication of the form `Basic <username:password>`.
          required: true
          schema:
            type: string
        - name: Accept
          in: header
          required: false
          schema:
            type: string
            default: application/json
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateOrderResponse'
        '415':
          description: Unsupported Media Type
          content: {}
        '422':
          description: >
            | errorCode | Description |

            | --- | --- |

            | invalid_object             | One or more required fields were
            missing or invalid.                     |

            | unsupported_payment_type   | The amount is outside of the
            merchant's payment limits, as returned by Get Configuration. |

            | unsupported_currency       | The amount is outside of the
            merchant's payment limits, as returned by Get Configuration. |
          content: {}
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/OrderRequest'
components:
  schemas:
    MoneyCurrency:
      type: string
      enum:
        - value: AUD
        - value: NZD
        - value: USD
        - value: CAD
        - value: GBP
    Money:
      type: object
      properties:
        amount:
          type: string
          description: >-
            The amount as a string representation of a decimal number, rounded
            to 2 decimal places.
        currency:
          $ref: '#/components/schemas/MoneyCurrency'
      required:
        - amount
        - currency
    InitiationActor:
      type: string
      enum:
        - value: CUSTOMER
        - value: MERCHANT
    Initiation:
      type: object
      properties:
        actor:
          $ref: '#/components/schemas/InitiationActor'
          description: The party who initiated the order
      required:
        - actor
    SubscriptionType:
      type: string
      enum:
        - value: FIXED
        - value: VARIABLE
    SubscriptionInterval:
      type: string
      enum:
        - value: DAY
        - value: WEEK
        - value: MONTH
        - value: YEAR
    Subscription:
      type: object
      properties:
        type:
          $ref: '#/components/schemas/SubscriptionType'
        interval:
          $ref: '#/components/schemas/SubscriptionInterval'
        intervalCount:
          type: integer
    Enrichments:
      type: object
      properties:
        initiation:
          $ref: '#/components/schemas/Initiation'
          description: Who initiated the order
        subscription:
          $ref: '#/components/schemas/Subscription'
          description: Metadata on the subscription details
      required:
        - initiation
    Consumer:
      type: object
      properties:
        email:
          type: string
          format: email
        givenNames:
          type: string
          description: The consumer's first name
        surname:
          type: string
          description: The consumer's last name
        phoneNumber:
          type: string
      required:
        - email
    ContactCountryCode:
      type: string
      enum:
        - value: AU
        - value: NZ
        - value: US
        - value: CA
        - value: GB
    Contact:
      type: object
      properties:
        name:
          type: string
        line1:
          type: string
          description: First line of the address
        line2:
          type: string
          description: 'Second line of the address '
        area1:
          type: string
          description: |-
            - AU: Suburb
            - NZ: Town or City
            - UK: Postal Town
            - US: City
            - Canada: City
        area2:
          type: string
          description: |-
            - NZ: suburb
            - UK: village or local area.
        region:
          type: string
          description: |-
            - AU: State 
            - NZ: Region
            - UK: County
            - US: State
            - CA: Province or Territory
        postcode:
          type: string
        countryCode:
          $ref: '#/components/schemas/ContactCountryCode'
        phoneNumber:
          type: string
          description: >-
            The phone number, in [E.123](https://en.wikipedia.org/wiki/E.123)
            format.
      required:
        - name
        - line1
        - area1
        - region
        - postcode
        - countryCode
    Item:
      type: object
      properties:
        name:
          type: string
        sku:
          type: string
        pageUrl:
          type: string
        imageUrl:
          type: string
        quantity:
          type: integer
        price:
          $ref: '#/components/schemas/Money'
        categories:
          type: array
          items:
            type: array
            items:
              type: string
        estimatedShipmentDate:
          type: string
          format: date
        preorder:
          type: boolean
          default: false
      required:
        - name
        - quantity
        - price
    Discount:
      type: object
      properties:
        displayName:
          type: string
        amount:
          $ref: '#/components/schemas/Money'
    ShippingCourierPriority:
      type: string
      enum:
        - value: STANDARD
        - value: EXPRESS
    Shipping-Courier:
      type: object
      properties:
        shippedAt:
          type: string
          format: date-time
        name:
          type: string
        tracking:
          type: string
        priority:
          $ref: '#/components/schemas/ShippingCourierPriority'
    OrderRequest:
      type: object
      properties:
        requestId:
          type: string
          format: uuid
          description: A unique identifier for the request, used for idempotency
        grantId:
          type: string
          description: ID of the grant used to create this order
        amount:
          $ref: '#/components/schemas/Money'
          description: The total amount of the order
        enrichments:
          $ref: '#/components/schemas/Enrichments'
          description: Describes additional fields beyond core payment information.
        consumer:
          $ref: '#/components/schemas/Consumer'
        billing:
          $ref: '#/components/schemas/Contact'
        shipping:
          $ref: '#/components/schemas/Contact'
        items:
          type: array
          items:
            $ref: '#/components/schemas/Item'
        discounts:
          type: array
          items:
            $ref: '#/components/schemas/Discount'
        merchantReference:
          type: string
          description: The reference/order ID in the merchant's system
        shippingAmount:
          $ref: '#/components/schemas/Money'
          description: The shipping cost amount
        taxAmount:
          $ref: '#/components/schemas/Money'
          description: The tax amount
        courier:
          $ref: '#/components/schemas/Shipping-Courier'
      required:
        - requestId
        - grantId
        - amount
        - enrichments
    CreateOrderResponse:
      type: object
      properties:
        token:
          type: string
        expires:
          type: string
          format: date-time
      required:
        - token
        - expires

```

## SDK Code Examples

```python
import requests

url = "https://global-api-sandbox.afterpay.com/v2/orders"

payload = {
    "requestId": "123e4567-e89b-12d3-a456-426614174000",
    "grantId": "005.2fkv5o963b132z8ppa34srlh60987y87d55re237wsg9tr4q2",
    "amount": {
        "amount": "100.00",
        "currency": "AUD"
    },
    "enrichments": { "initiation": { "actor": "CUSTOMER" } }
}
headers = {
    "Authorization": "Basic <username>:<password>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript
const url = 'https://global-api-sandbox.afterpay.com/v2/orders';
const options = {
  method: 'POST',
  headers: {
    Authorization: 'Basic <username>:<password>',
    'Content-Type': 'application/json'
  },
  body: '{"requestId":"123e4567-e89b-12d3-a456-426614174000","grantId":"005.2fkv5o963b132z8ppa34srlh60987y87d55re237wsg9tr4q2","amount":{"amount":"100.00","currency":"AUD"},"enrichments":{"initiation":{"actor":"CUSTOMER"}}}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://global-api-sandbox.afterpay.com/v2/orders"

	payload := strings.NewReader("{\n  \"requestId\": \"123e4567-e89b-12d3-a456-426614174000\",\n  \"grantId\": \"005.2fkv5o963b132z8ppa34srlh60987y87d55re237wsg9tr4q2\",\n  \"amount\": {\n    \"amount\": \"100.00\",\n    \"currency\": \"AUD\"\n  },\n  \"enrichments\": {\n    \"initiation\": {\n      \"actor\": \"CUSTOMER\"\n    }\n  }\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Basic <username>:<password>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://global-api-sandbox.afterpay.com/v2/orders")

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

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Basic <username>:<password>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"requestId\": \"123e4567-e89b-12d3-a456-426614174000\",\n  \"grantId\": \"005.2fkv5o963b132z8ppa34srlh60987y87d55re237wsg9tr4q2\",\n  \"amount\": {\n    \"amount\": \"100.00\",\n    \"currency\": \"AUD\"\n  },\n  \"enrichments\": {\n    \"initiation\": {\n      \"actor\": \"CUSTOMER\"\n    }\n  }\n}"

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

```java
HttpResponse<String> response = Unirest.post("https://global-api-sandbox.afterpay.com/v2/orders")
  .header("Authorization", "Basic <username>:<password>")
  .header("Content-Type", "application/json")
  .body("{\n  \"requestId\": \"123e4567-e89b-12d3-a456-426614174000\",\n  \"grantId\": \"005.2fkv5o963b132z8ppa34srlh60987y87d55re237wsg9tr4q2\",\n  \"amount\": {\n    \"amount\": \"100.00\",\n    \"currency\": \"AUD\"\n  },\n  \"enrichments\": {\n    \"initiation\": {\n      \"actor\": \"CUSTOMER\"\n    }\n  }\n}")
  .asString();
```

```php
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://global-api-sandbox.afterpay.com/v2/orders', [
  'body' => '{
  "requestId": "123e4567-e89b-12d3-a456-426614174000",
  "grantId": "005.2fkv5o963b132z8ppa34srlh60987y87d55re237wsg9tr4q2",
  "amount": {
    "amount": "100.00",
    "currency": "AUD"
  },
  "enrichments": {
    "initiation": {
      "actor": "CUSTOMER"
    }
  }
}',
  'headers' => [
    'Authorization' => 'Basic <username>:<password>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp
var client = new RestClient("https://global-api-sandbox.afterpay.com/v2/orders");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic <username>:<password>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"requestId\": \"123e4567-e89b-12d3-a456-426614174000\",\n  \"grantId\": \"005.2fkv5o963b132z8ppa34srlh60987y87d55re237wsg9tr4q2\",\n  \"amount\": {\n    \"amount\": \"100.00\",\n    \"currency\": \"AUD\"\n  },\n  \"enrichments\": {\n    \"initiation\": {\n      \"actor\": \"CUSTOMER\"\n    }\n  }\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "Authorization": "Basic <username>:<password>",
  "Content-Type": "application/json"
]
let parameters = [
  "requestId": "123e4567-e89b-12d3-a456-426614174000",
  "grantId": "005.2fkv5o963b132z8ppa34srlh60987y87d55re237wsg9tr4q2",
  "amount": [
    "amount": "100.00",
    "currency": "AUD"
  ],
  "enrichments": ["initiation": ["actor": "CUSTOMER"]]
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://global-api-sandbox.afterpay.com/v2/orders")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```