Many SAS Solutions use APIs to interface with 3rd party software or other SAS solutions. For instance, there’s a REST API for SAS® Anti-Money Laundering that retrieves a list of unique scoring dates for a specific customer. And, in SAS® Visual Investigator, you can use a REST API to create a data store connection, instead of creating the connection manually. The possibilities are endless with what you can do with APIs, however, APIs can be difficult to understand at first! In this post, I’ll start with the basics and give an introduction to APIs.
API stands for application programming interface.
APIs are a set of rules and protocols that enable different software components to communicate with each other. To us, APIs simply look like code or a particular programming language, but for computers, APIs provide a way to share data or functionality. An API works as a facilitator connecting software platforms that would otherwise not be able to interact, which enhances the user’s experience.
You probably use APIs daily without even knowing. For example, when you check the weather app on your phone, it’s most likely using an API to get the most current weather data from a service. Or, if you’re online shopping, and you use an online payment method. These payment systems are embedded into many websites that require financial transactions. The website interacts with the system, which connects to your personal financial information. The website itself does not have direct access to your financial information.
A common metaphor used to understand an API is ordering food at a restaurant. This metaphor involves you (the customer), the waiter, and the chef. You represent the client, the waiter in this metaphor represents the API, and the chef represents the server.
Let’s say that the waiter comes to your table, and you order a burger. The waiter gets your request for a burger and takes it to the chef, the person who’s making the burger. The waiter just helps you out with the different requests. When the burger is done, your waiter brings you food in the form of a burger, and you get to consume that burger in any way, shape, or form (that is, using your hands or using utensils).
Just like the waiter is the facilitator to connect you to the chef, APIs are the facilitators between software platforms to interact.
So far, I've covered a very brief overview of APIs. Let's now review an example of how to create a new Visual Investigator datastore by using a REST API request. I use Visual Studio Code in this example, but you can use any API application.
As with any REST API endpoint, I first need a valid access token to authenticate the request, which I’ve already obtained. There are many ways to obtain an access token. Please review the Initial Steps section of Maggie Redford's post for one example: Getting started with the REST APIs of Visual Investigator.
I’ll also need the credentials for the database I intend to connect to Visual Investigator with the newly created data store. I already have this information as well.
I’ll create a new request in Visual Studio Code by clicking File, New File. I enter create_datastore.http for the file name, and press enter. Note the http extension. Then, I click Create File.
Next, I define a few variables that will be used in the request.
The first variable is the baseUrl. In the file, I enter @baseURL = https://server.demo.sas.com. Next, I’ll define the access token, by entering @accessToken= and then copying the access token I obtained previously.
I enter POST and then add a reference to my base url and add the endpoint, which is svi-datahub/admin/dataStores.
On the next line, I add the Content-Type header and enter application/json. Next, I add the Authorization header and enter Bearer and reference my access token.
I add an extra line before I enter the request body. Do not forget this extra line!
I add the name of the database, which in this example is insurance. Then, I enter the database type, which is RELATIONAL, and the version which is 0.
Next, I add the connectionType, which is postgres and the host, which is server.demo.sas.com.
Then, I add the port number, which is 30432 and the username and password of the database we are connecting to, which is pgadmin and Student1.
Then, I add the databaseNameOrServiceName which is postgres, and the schema which is insurance.
Lastly, I include urlAppendedParameters which is ssl=prefer.
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
Note the quotation marks in the request body.
After I save this request, I right-click and select Send Request. The database is then created!
This was a very brief overview of APIs. To learn more, please see the SAS developers documentation for REST APIs: Rest APIs | SAS
Below are the resources I used for this post:
Brewster, Cordenne. 2021. “7 Examples of APIs in Use Today.” trio (blog). https://www.trio.dev/back-end/resources/api-examples. Last modified August 13, 2021.
Khalid, Yasoob. 2023. “API For Dummies: Learning The Basics Of API.” ScrapingBee (blog). https://www.scrapingbee.com/blog/. Last modified January 29, 2023.
Taulien, Kalob. 2020. “The simplest way to think about an API (the metaphor).” O’Reilly Media, Inc. https://learning.oreilly.com/videos/understanding-apis-and/9781800564121/9781800564121-video2_1/
Find more articles from SAS Global Enablement and Learning here.
Hello All.
I can't get my code to work and the only thing I get in my log is ERROR: The tcpSockRead call failed. The system error is 'The connection was reset by a peer.'.
ERROR: Call to tcpSockContinueSSL failed.
For this API I need to do a post to get my authorization token and then a get with the token to get my data. Any help would be appreciated.
%let client_id ='XXXXX1f-471b-XXX-a2c4XXXXXX';
%let client_secret = 'XXXXYfoXXXXXdC0BTZD5XXXX';
data _null_;
length newString $200.;
newString=cats("&client_id.",":","&client_secret.");
call symputx('Basic_Auth',put(trim(newString),$base64x300.),'g');
run;
%put &Basic_Auth;
proc http
url = 'https://api.NA2.cmpy.com/oauth2/token'
method = "post"
in='grant_type=client_credentials'
out = out
AUTH_BASIC
ct="application/x-www-form-urlencoded";
headers
"x-api-key"= "ZZe7f3fc-8946-ZZZf-9d85-9ZZZZ1c2860"
"Authorization" = "&Basic_Auth.";
debug level= 3;
run;
@debrahudson Make sure that HTTP calls to the internet work on your system in general. See this blog post for information and a test that you can run.
Thanks Chris for your response. I have version SAS EG 8.3 and the comments say I need 9.4 so Im not sure if that is the issue but I did get the same error as my API code.
ERROR: The tcpSockRead call failed. The system error is 'The connection was reset by a peer.'.
ERROR: Call to tcpSockContinueSSL failed.
@debrahudson Your version of EG doesn't matter so much as it just connects to SAS. I suspect that your problem might be that you cannot get to the Internet from your SAS environment. Usually a PROXY host is required in that case -- a gateway that allows you to make API calls outside of your firewall. You might need to consult with your IT support for the gateway/proxy information. Then use the instructions I linked to in the article.
thanks Chris. I will go ahead and do that.
@ChrisHemedinger Are there any MailChimp API examples. I would like to convert our process from R to SAS.
Thank you.
@ChrisHemedinger I got the API post working and got the access token. However when I put the access token in my get API I get the following error: The quoted string currently being processed has become more than 262 bytes long. Any help would be appreciated.
Hi @debrahudson , that usually shows as a WARNING but it could be treated as an error if you enable some strict SAS options.
To bypass, add:
options noquotelenmax;
before your API calls. If you want to reset it at after these steps, add:
options quotelenmax;
More info here about QUOTELENMAX.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.