After we had a look at how to sent push notifications via C# recently, there are plans for the next C# article today. This time it’s about how to read account balance, sales and transactions and other things of a Paypal account.
First some basic information. Although Paypal is an “online account”, in many ways it differs from an online (giro) account of a “classic” bank. For example, Paypal does not support the Homebanking Computer Interface (HBCI) to retrieve account information or send payments. Instead, Paypal offers different APIs (REST, SOAP, + SDKs, …), which differ not only in structure and technology, but especially in the range of functions. For our purposes we will use the so-called “NVP / SOAP API” and connect to it with C# and Visual Studio standard means. We won’t use any special libraries or SDKs.
Preparations – Paypal API Keys
Before we start programming, we still need the access data, which allow us access via the interface. Access via your normal Paypal address and password is not provided for use with the SOAP API.
The API access data can be requested/generated via the following links. To register on the website, you must use the access data of your normal Paypal account, for which you want to retrieve information later. Depending on the age of your account, there are two different ways to generate the API key.
Method 1: Get Paypal SOAP-API credentials
Method 2: Get Paypal SOAP-API credentials (new)
If you take the link from variant 1 and if you are redirected to a slightly “stale” website that displays API username, signature, and API password, then you’re ready. Copy the values that you can make visible by clicking on the “Show” links. Variant 1 corresponds to the screenshot shown on the right side of this paragraph.
If the link does not lead you to the API key page, then please select the second of the two links I posted above. In this case, you need to scroll to the bottom of the page and click “Create / Manage API Permission” in the “NVP / SOAP API Integration (Classic)” section. The following screenshots show the procedure. (Click screenshots to enlarge.)
After you have created the keys, also in this variant you have to copy the API user, the API password and the signature. All three will be needed for C# programming later.
Tip: Save the above mentioned links or bookmark this blog article. You’ll thank me later, because the link is so bad to find on the Paypal site that there are multiple articles on StackOverflow that deal with how to get to those links …
Preparations Pt. II – GUI and webservice class model
Let us now turn to implementation in C#. As a supporting program, I created a new WinForms project in Visual Studio and added a Richtext box named “richTextBoxOutput” as well as a button. We will use the button’s click event to trigger the Paypal access later. The application interface corresponds to the screenshot on the left side.
For easier use of the Paypal-API, we will automatically generate a class model for access to the API using Visual Studio. (As previously announced, in this article we use only on-board tools and no additional classes or libraries.)
To do this, right-click on the menu item “References” in the project folder explorer of the Visual Studio and select the option “Add service reference”. In the field “Address:” we enter the url to the WSDL of the Paypal-SOAP-API:
https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl
By clicking on “Go to” we load the API model into Visual Studio. Then we change the namespace in the field of the same name from “ServiceReference1” to “ServicePaypal”. (Theoretically, you can choose any namespace. The change is only for clarity in the later coding.) Finally, we have the (service) class model with a click on the “Ok” button generate.
To do this you need to open the “App.config” file in the Solution Explorer and search for the part beginning with the following text:
<endpoint address=
Swap the urls stored there (with and without “aa”) against the following two urls, if you want to program against the real Paypal server.
https://api-3t.paypal.com/2.0/ https://api-aa-3t.paypal.com/2.0
Now that we’ve created the class model and adjusted the endpoints, we can finally move to querying Paypal for information.
Get information from Paypal-API in C#
I wrote the following code in the button-click method of the example application we set up earlier. In principle, the code can of course be used at any point in the program. In the first step, we want to query the account balance of the Paypal account and display it in the Richtext box. The flow of the program can be read from the code comments.
var user = "XXXXXXXgooglemail.com"; var pass = "XXXXXXXX"; var signature = "XXXXXXXX"; try { //General init var paypalService = new ServicePaypal.PayPalAPIInterfaceClient(); //Create authentication header var securityHeader = new ServicePaypal.CustomSecurityHeaderType(); var credentials = new ServicePaypal.UserIdPasswordType(); credentials.Username = user; credentials.Password = pass; credentials.Signature = signature; //Set security header securityHeader.Credentials = credentials; //Create balance request var payloadReqBal = new ServicePaypal.GetBalanceReq() { GetBalanceRequest = new ServicePaypal.GetBalanceRequestType() }; //Configure balance request payloadReqBal.GetBalanceRequest.Version = "204.0"; payloadReqBal.GetBalanceRequest.ReturnAllCurrencies = "1"; //Get balance var balance = paypalService.GetBalance(ref securityHeader, payloadReqBal); //Show balance for each currency foreach (var balanceLocal in balance.BalanceHoldings) { //Show balance (Value = X.XX, currencyID = "EUR", etc.) richTextBoxOutput.AppendText($"{balanceLocal.Value} {balanceLocal.currencyID}rn"); } } catch (Exception ee) { MessageBox.Show($"Houston, we had a problem: {ee.Message}"); }
The above code queries the account balance and outputs it in the form X.XX {currency code} in the Richtext box. The variables “user”, “pass” and “signature” have to be filled with the API access data, which we have determined in the first part of this article. Apart from the security header, the retrieval of the account balance is otherwise quite easy.
Let’s get to the transactions. The first part of the code is similar to that for the account balance. Reading out the transactions itself, but then deviates slightly. But let’s just have a look at the code.
var user = "XXXXXXXgooglemail.com"; var pass = "XXXXXXXX"; var signature = "XXXXXXXX"; try { //General init var paypalService = new ServicePaypal.PayPalAPIInterfaceClient(); //Create authentication header var securityHeader = new ServicePaypal.CustomSecurityHeaderType(); var credentials = new ServicePaypal.UserIdPasswordType(); credentials.Username = user; credentials.Password = pass; credentials.Signature = signature; //Set security header securityHeader.Credentials = credentials; //Create transaction request var payloadReq = new ServicePaypal.TransactionSearchReq() { TransactionSearchRequest = new ServicePaypal.TransactionSearchRequestType() }; //Set last 15 days as period for the transaction log //If you want you can set an optional ".EndDate" payloadReq.TransactionSearchRequest.StartDate = DateTime.Now.AddDays(-15); payloadReq.TransactionSearchRequest.Version = "204.0"; //Get the list of transactions var transactionList = paypalService.TransactionSearch(ref securityHeader, payloadReq); //Check if transactions were transferred if (transactionList != null && transactionList.Ack == ServicePaypal.AckCodeType.Success && transactionList.PaymentTransactions != null) { //Show each transaction in richtextBox foreach (var trans in transactionList.PaymentTransactions) { var businessPartner = !string.IsNullOrEmpty(trans.PayerDisplayName) ? trans.PayerDisplayName : trans.Payer; var output = $"Amount: {trans.GrossAmount.Value}rn"; output += $"Paypal-Fees: {trans.FeeAmount.Value}rn"; output += $"Transaction type: {trans.Type}rn"; output += $"Time of transaction: {trans.Timestamp.ToShortDateString()}rn"; output += $"Transaction contact: {businessPartner}rnrn"; richTextBoxOutput.AppendText(output); } } } catch (Exception ee) { MessageBox.Show($"Houston, we had a problem: {ee.Message}"); }
The code above retrieves the transactions / transfers of the last 15 days. If desired, an end date can be set next to the start date so that only a specific section of the past is considered. In the foreach loop to the end of the snippet, each individual transaction is shown. As an example, I have added a few different values for each transaction like the payee / sender (= businessPartner variable) or the Paypal fees (= trans.FeeAmount.Value) to the output. A complete overview of the available properties can be found here in the Paypal API documentation.
Conclusion
In principle, the access to Paypal via C# is not really difficult. Apart from authentication, access has been done in a few lines. What makes the whole thing quite cumbersome, is the confusing documentation of the API. This starts with the fact that you only get to the API-Keys in a complicated way and stops with the fact that even the documentation page for the Paypal API (I linked it in the last paragraph) is not really clearly structured. For example I personally miss an overview of the individual methods of the service in the menu of the documentation website.
What are your experiences with the Paypal API? Have you already worked with the API or maybe with other FinTech APIs? I am happy as always to dicuss and exchange experiences. (Constructive criticism and comments on the article are also welcome!)
Engage with church ministry services designed to deepen your relationship with God and others. Be part of a church ministry committed to outreach, worship, and empowering believers in Christ.
How to budget practical advice for tracking expenses, managing income, and building a solid financial foundation. Master how to budget with simple techniques to control spending, increase savings, and achieve your financial goals.
I received my first statement for the month of July but the message states I am not authorized to open and read the contets
I think I have followed every step. But I have been getting the same error message “Could not establish secure channel for SSL/TLS with authority ‘api.sandbox.paypal.com’. I copied and pasted the user name, password and signature from my developer paypal account to my code. But still keep getting the same error. Please help!