Logo Lanfrica

safaricom/mpesa-api-C-Sharp

Domain:

digital infrastructure

Record type:

software
Creator:
saf
Host:
C# Libraries # MPESA Daraja Api C# libraries. ## Introduction This package seeks to help C# developers implement the various MPESA APIs without much Challenge. It is based on the REST API whose documentation is available on developer.safaricom.co.ke. ## Features 1. Supports HTTP request 2. Support HTTP response ## Requirements Microsoft .NET Framework 4.6.2 or higher, Microsoft Visual C++ 2017 Redistributable (x86). It is yet to be tested with lower versions of the above. ## Basic 1. Create a Webrequest instance by calling Create with the URI of the resource. ``` String a = "sandbox.safaricom.co.ke"; String baseUrl = a; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseUrl); ``` ### Note The .NET Framework provides protocol-specific classes derived from WebRequest and WebResponse for URIs that begin with "http:" or "https:'' To access resources using other protocols, you must implement protocol-specific classes that derive from WebRequest and WebResponse. 2. Set any property values that you need in the WebRequest. For example, • to enable authentication, ``` String token = "ACCESS_TOKEN"; request.Headers.Add("authorization", "Bearer " + token); ``` • Set the method to be used mostly GET or POST. In daraja .When generating the access token the GET method is used while the POST is used with almost the rest of the other APIs. ``` request.Method = "POST"; or request.Method = "GET"; ``` • To set the content type to be used in daraja json is used. ``` request.ContentType = "application/json"; ``` Generate the API Body depending on the Api parameter requirements eg for b2c we will use : ``` string json = "{\"InitiatorName\":\" here\"," + "\"SecurityCredential\":\" here \"," + "\"CommandID\":\" here \"," + "\"Amount\":\" here \"," + "\"PartyA\":\" here \"," + "\"PartyB\":\" here \"," + "\"Remarks\":\" here \"," + "\"QueueTimeOutURL\":\"your_timeout_url"," + "\"ResultURL\":\"your_res

Languages