Skip to content

Make Your First Call

Jason Lee edited this page Jul 22, 2015 · 2 revisions

Once you have installed and configured the SDK, you are ready to make your first call. The following steps will guide you through this process:

1. Authenticate With PayPal

Before you can begin making various calls to PayPal's REST APIs via the SDK, you must first authenticate with PayPal using an OAuth access token that can be used with each call. To do this, you will need to use the OAuthTokenCredential class.

using PayPal.Api;

// Get a reference to the config
var config = ConfigManager.Instance.GetProperties();

// Use OAuthTokenCredential to request an access token from PayPal
var accessToken = new OAuthTokenCredential(config).GetAccessToken();

NOTE: It is not mandatory to generate the accessToken with every call using the SDK. Typically, the access token can be generated once and reused until it expires.

For more information on the access token and how it is used in calls to PayPal, refer to How PayPal Uses OAuth 2.0.

2. Configure an APIContext Object

The APIContext class makes it easy for developers to customize how calls are being made to PayPal. Every PayPal resource method available with this SDK uses an APIContext object.

var apiContext = new APIContext(accessToken);

This object can be further setup to include custom configuration settings as well as include specific HTTP headers, allowing for more flexibility and customization in how HTTP requests are made using this SDK.

// Initialize the apiContext's configuration with the default configuration for this application.
apiContext.Config = ConfigManager.Instance.GetProperties();

// Define any custom configuration settings for calls that will use this object.
apiContext.Config["connectionTimeout"] = "1000"; // Quick timeout for testing purposes

// Define any HTTP headers to be used in HTTP requests made with this APIContext object
if(apiContext.HTTPHeaders == null)
{
  apiContext.HTTPHeaders = new Dictionary<string, string>();
}
apiContext.HTTPHeaders["some-header-name"] = "some-value";

NOTE: When using APIContext.HTTPHeaders, be aware that some headers will be overwritten depending on the SDK call (e.g. Authorization, Content-Type, and User-Agent).

3. Make an SDK Call

Now that you have your APIContext object setup, it's time to make your first call with the SDK. The following is a simple example of how to get the details of a specific PayPal payment.

var payment = Payment.Get(apiContext, "PAY-0XL713371A312273YKE2GCNI");