Create Terminal action from custom Swift application

I am building a custom checkout application that will use the Square Terminal. I have registered the application on my Square Developer account and followed the tutorial steps to connect the terminal to this application. I want to be able to send an amount of money to charge to my square terminal so that it can complete the transaction on behalf of my app.

I am looking at this documentation developer.squareup.com/reference/square/terminal-api/create-terminal-action

If you go to the above link it gives code example of how to send a transaction in cURL. This code is the following:
curl https://connect.squareup.com/v2/terminals/actions
-X POST
-H ‘Square-Version: 2022-06-16’
-H ‘Authorization: Bearer ACCESS_TOKEN’
-H ‘Content-Type: application/json’
-d ‘{
“idempotency_key”: “thahn-70e75c10-47f7-4ab6-88cc-aaa4076d065e”,
“action”: {
“device_id”: “{{DEVICE_ID}}”,
“type”: “SAVE_CARD”,
“deadline_duration”: “PT5M”,
“save_card_options”: {
“customer_id”: “{{CUSTOMER_ID}}”,
“reference_id”: “user-id-1”
}
}
}’

It also gives the code to make this request in Ruby, Python, C#, Java, PHP and Node.js. My question is what would the equivalent code look like for Swift?

For context, the app I’m developing is for a supervised self-checkout kiosk. It will run on an Ipad that has access to the internet. My app will determine the final amount to charge and that amount will be sent to the terminal to complete the payment.

:wave: Are you wanting to charge the card or store the card on file. What you provided is for storing the card on file. If you want to charge a card you’ll need to call CreateTerminalCheckout.

Also these call to create either a Terminal checkout or action to save a card on file are server side API calls which is why the examples are in Ruby, Python, C#, Java, PHP and Node.js. You’ll need to have your Swift client side app make a request to your server which will make the call to Square and handle any response. :slightly_smiling_face:

Yes my bad, I grabbed that code from the wrong page. I want to charge the card, not store it on file.

And thanks a lot! I’m super new to all this stuff. To have a backend for my app, I set up Firebase and linked it to my app using the firebase sdk. Firebase is a backend-as-a-service platform that provides a database as well as a bunch of other functionality for your app. One of the things they offer is cloud functions which are snippets of code can be triggered by directly by my app, or set to be triggered when the database has been modified (like recording a transaction). This is the link to Firebase functions: Cloud Functions for Firebase | Run your mobile backend code without managing servers

I tried the sample code provided to make a cURL request through command line and that successfully sent a transaction to my square terminal. I followed the Firebase instructions to set up and deploy functions with node.js but I’m a little bit confused as to how I should use the node.js code snippet provided to make a terminal checkout request (I’m pretty new to node.js and making network requests). Here is the snippet for convenience:

try {
const response = await client.terminalApi.createTerminalCheckout({
idempotencyKey: ‘28a0c3bc-7839-11ea-bc55-0242ac130003’,
checkout: {
amountMoney: {
amount: 2610,
currency: ‘USD’
},
referenceId: ‘id11572’,
note: ‘A brief note’,
deviceOptions: {
deviceId: ‘dbb5d83a-7838-11ea-bc55-0242ac130003’
}
}
});

console.log(response.result);
} catch(error) {
console.log(error);
}

I’ll be able to use the tool Square provides to customize the code snippet to work for my device and app like I did with getting the cURL request to work from my command line. What I’m not sure about is how to incorporate the snippet as a function. Do I first need to define/instantiate ‘client’ so it knows where to send the checkout request? With the curl command, the first line was
curl https//connect.squareup.com/v2/terminals/checkouts which gave the location to send the json checkout information but I’m not sure how to include the URL with node.js.

One other thing is that in the ‘helloworld’ example Firebase gave for how to write cloud functions, the code looked like this:

const functions = require(“firebase-functions”);
exports.helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info(“Hello logs!”, {structuredData: true});
response.send(“Hello from Firebase!”);
});

I’m assuming the exports.helloWorld part is what exports the function to the cloud. Would this change how I need to write the checkout request?

Thanks a lot for all your help!

:wave: When calling Square APIs with the SDK you’ll have to define the client so Square knows what account to associate the charge with. As for the questions about the Firebase functions I’d recommend reaching out them for assistance. :slightly_smiling_face: