devxlogo

Create a Modern Extension to Your Online Business with a Public API

Create a Modern Extension to Your Online Business with a Public API

or several years now, the use of affiliate networks in e-commerce has grown tremendously. Amazon, the vanguard of this technique, and many companies like it allow anyone who can consume a Web service to become an affiliate. Being an affiliate means you sell goods on their behalf?for a cut of the profit.

From a technological point of view, it’s a bold leap for these retailers?effectively opening up their data centers and their business processes to outsiders. But with a bold leap comes the potential of a rich reward, and it is this reward?an enormous extension of their market reach, with the bulk of the work done by others?that justifies any risk.

The technology that enables this extra reach is the technology of Web services, which is based on these pillars:

  • Extensible Markup Language (XML)?A text-based format that allows data to be richly attributed and tagged. Most importantly, it is machine-readable and parseable.
  • Simple Object Access Protocol (SOAP)?An XML variant that may be used to describe how you call functionality on a Web service and the data that is returned from it.
  • Web Services Definition Language (WSDL)?An XML variant that describes a Web service and how you can talk to it. Again this is machine readable, so you can use this to automatically generate code that can talk to Web services and understand their responses using SOAP.
  • Universal Description, Discovery and Integration (UDDI)?A directory standard where you can publish your Web service WSDL for others to find and consume.

When using these technologies, the process of opening up your business processes to the public in a secure manner becomes relatively easy. With a bit of thought, most online companies can begin to publish their content, their products, or their services publicly through an API, so that an affiliate network can grow and sell those products or services.

What you’ll learn in this article will turn the affiliate relationship on its head?putting your business in the driver’s seat and using Web services to create an affiliate or similar program that can grow your business. In other words, if you already have a viable business, and you want to do the same thing as the Amazons and PayPals of the world, you can create your own API that allows affiliates to use your business resources. In this article, you’ll build a simple commercial Web site using C# and Web services, and see how these can be exposed to your clients so that they can become affiliates that sell your goods on your behalf and get a percentage that you determine.

Architecture of a Simple Store
The code available in the download (get it here or from the link in the left-hand column) contains the implementation and database setup scripts of a very simple online store that provides affiliation. The database has a number of tables:

  • Products: Details of the products that you sell.
  • Affiliates: Details of the affiliates that you have signed up.
  • Sales: Details of a sales transaction including the affiliate that made the sale.

In addition to this there are a number of Web services that use this data. These Web services are the core of the API that you will offer to your affiliates.

  • Affiliate Sign Up: Handles the signing up of affiliates, adding them to the database, and assigning them an identifier.
  • Affiliate Sale: Handles a sale made by an affiliate. The affiliate will pass the customer information to your site, which will then process the transaction, generating a transaction reference number that is returned to the affiliate to return to their client?the one who is purchasing the goods.
  • Catalog Query: Used by an affiliate to query current inventory so they can offer those products to their customers in real time.
  • Check Specific Item is in Stock: The Catalog Query tells the affiliate whatyou have in stock. However, in the time lag between this query being run and a specific customer transaction, stock levels may change. This API call lets the affiliate check inventory again before finalizing the transaction with its customer.

These Web services have no user interface. You can create whatever front end you like to access them, as can your affiliates, allowing custom branding to be built. Some samples of Web forms that could be used by either you or your affiliates are available in the download.

Constructing an Affiliate Sign Up Service
You need to set up your Web site to host the affiliate sign-up service and the associated Web forms to facilitate sign up. This is where you invite potential affiliates to sign up, get an affiliate ID, and start trading using your Web services-based API. To create the database, you can use the SQL scripts that are available in the download.

You’ll need some code that takes data in and writes it to the database, getting an automatically generated ID back from SQL Server. This code is shown below. The workspace also contains the definition for a class called AffiliateClass, which encapsulates the data for an affiliate. In the code below, an instance of this class called affThis is used.

int nReturn = 0;sqlConnection2.Open();sqlInsertCommand1.Parameters["@Name"].Value=affThis.UserName;sqlInsertCommand1.Parameters["@Description"].Value=                                         affThis.UserDescription;sqlInsertCommand1.Parameters["@Address1"].Value=affThis.Address1;sqlInsertCommand1.Parameters["@Address2"].Value=affThis.Address2;sqlInsertCommand1.Parameters["@City"].Value=affThis.City;sqlInsertCommand1.Parameters["@State"].Value=affThis.State;sqlInsertCommand1.Parameters["@Country"].Value=affThis.Country;SqlDataReader r = sqlInsertCommand1.ExecuteReader();if(r.Read())  nReturn = Convert.ToInt16(r["AffiliateID"].ToString());else  nReturn = 0;sqlConnection2.Close();return nReturn;

Using Visual Studio.NET it is simple to construct a Web Form that allows the user to enter data and then call the Web service using the entered values. To begin, you have to create a reference to the Web service. In this case, the form, the service, and the database all run on the same development machine. Figure 1 shows how you would add a reference to the Web service that handles the data entry (mentioned above). This generates a Web services proxy that you can use in your Web Form code to pass the data to the Web service.

HostSVC.AffiliateClass affThis = new HostSVC.AffiliateClass();affThis.UserName = txtName.Text;affThis.UserDescription = txtDescription.Text;affThis.Address1=txtAddress1.Text;affThis.Address2=txtAddress2.Text;affThis.City=txtCity.Text;affThis.Country=txtCountry.Text;affThis.State=txtState.Text;HostSVC.API theAPI = new HostSVC.API();int nID = theAPI.NewAffiliate(affThis);if(nID==0){  lblStatus.Text = "Update Rejected. Please hit REFRESH and try again";}else{  lblStatus.Text = "Update Successful, your AffiliateID is :" + nID;}
Author’s Note: In a production system you should have extensive data validation to prevent SQL injection attacks as well as comprehensive error handling. Both have been omitted in this tutorial for the sake of clarity.

In a production system, you would want to send an email confirmation sent to the affiliate after sign up, but for the sake of brevity, this system just displays the confirmation on screen, along with the new ID that has been assigned to this affiliate.

Constructing a Sales Interface API
Now that your affiliate has an ID, they need to know what they can sell. They do this by calling the Catalog Query Web service, which gives them a list of all products currently in stock (Quantity > 0). You could make this a little more sophisticated by breaking your products down into different categories and building the API accept a category parameter. For the sake of simplicity, that is omitted here, and all products that are in stock are returned.

The Web Method below returns all the current inventory:

string strConnectionString = "integrated security=SSPI;   data source=(local);persist security info=True;   initial catalog=AffilliatePOC";SqlConnection con = new SqlConnection(strConnectionString);	int nSize = 0;string sql = @"SELECT COUNT(*) from Products Where Quantity>0";SqlCommand sqlCountProducts = new SqlCommand(sql,con);con.Open();nSize = Convert.ToInt16(sqlCountProducts.ExecuteScalar().ToString());ProductClass[] pReturn = new ProductClass[nSize];int nIndex = 0;sql = "Select * from Products Where Quantity>0";SqlCommand sqlSelectProducts = new SqlCommand(sql,con);SqlDataReader r = sqlSelectProducts.ExecuteReader();while(r.Read()){  pReturn[nIndex] = new ProductClass();  pReturn[nIndex].ID = Convert.ToInt16(r["ProductID"]);  pReturn[nIndex].Description = r["Description"].ToString();  pReturn[nIndex].ImageURL = r["ImageURL"].ToString();  pReturn[nIndex].Price = Convert.ToInt16(r["Price"]);  pReturn[nIndex].Quantity = Convert.ToInt16(r["Quantity"]);  pReturn[nIndex].Weight = Convert.ToInt16(r["Weight"]);  nIndex++;}con.Close();return pReturn;

This code uses a ProductClass helper class, which encapsulates the data points associated with a product and is directly analogous to the fields on the Product table in the database.

At this point, your affiliate has to do a little work to consume the service: Using this Web service endpoint, they could use Visual Studio.NET (or equivalent) to generate a proxy to your Web service by adding a Web services reference (see Figure 1). It does this by looking at the WSDL for the Web service and parsing it to get the names of the methods and their signatures (the parameters that they accept and return), and generating C# source code for a class that has the same methods with the same signatures. You can then talk to the proxy class, which talks to the Web service on your behalf.

Figure 1. Picture This: The screenshot shows the dialog in Visual Studio.NET for adding a reference to your Web services API from the affiliate point of view.

They can then build a user interface that consumes this proxy to render the available products. The getProducts() method (as you can see in Figure 1) returns an Array of type ProductClass. You can do whatever you like with this array, but in the download you will see the BuildXML function that creates an XML document out of this array. This XML document is then loaded into a DataSet, which can be bound to a Repeater control.

HostSVC.AffAPI theAPI = new HostSVC.AffAPI();pList = theAPI.getProducts();XmlDocument xmlDoc = BuildXML();DataSet dstVals = RenderData(xmlDoc);Repeater1.DataSource = dstVals;Repeater1.DataBind();

The repeater control is hosted on an ASPX page and generates a table entry for each item in the recordset. In addition, the repeater adds a button control to the table, which allows the user to ‘Buy’ the item in the table.

The ASP.NET markup is shown here:

                      <%# DataBinder.Eval(Container.DataItem, "Description") %>                    <%# DataBinder.Eval(Container.DataItem, "Quantity") %>                                  

Figure 2 shows an example of the page that would result from calling the API that exposes your product list.

When the user clicks the Buy button, the system needs to check if the item is still in stock (stock levels may have changed since the Catalog Query was run). This is done using the checkStock method on the Web service API, and assuming that the product is still in stock, then the specifics of the order are gathered and passed to the API for realization:

private void Repeater1_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e){  // Hard coding 'my' affiliate ID  // In a real system this would be determined differently.  int nAffiliateID = 5;  int n = e.Item.ItemIndex;  int nProductID = pList[n].ID;  HostSVC.AffAPI theAPI = new HostSVC.AffAPI();  HostSVC.Customer theCustomer = new HostSVC.Customer();  theCustomer = getCustomerDetails();  if (theAPI.CheckStock(nProductID))  {    theAPI.MakePurchase(nAffiliateID,nProductID,theCustomer);  }  else  {    // Display some kind of out of stock notification  }}
Figure 2. Making a Sale: The ASPX page shown is a simple example of a Web page that consumes the API that exposes your product inventory and allows an end user to make a purchase request.

In a real world scenario, you may have a sign-in screen prior to getting to the purchase, so you’ll have your customer details cached. Alternatively, if you don’t have any customer details cached, you could direct them to a screen where they have to be entered. In the download, the getCustomerDetails function simulates this by returning a hard-coded customer.

Back on your site, the Web service API has to handle this order request. You should check and charge the customer’s credit card before touching the database. In this article, I’ve assumed you’ve done that piece; and the next step is to update the database. This involves four steps:

  1. Getting the quantity available for each specific product.
  2. Reducing the quantity by the number purchased (in the example this is hard-coded to ‘1’), and then updating the inventory database with the new quantity.
  3. Updating the transactions table with the details of the sale, including the affiliate ID, so you can track which affiliate sold what.
  4. Shipping the product to the customer. (In an ideal case this would be a sub-step of step 2, as the inventory shouldn’t be reduced until it has shipped.)

The code for this is shown here:

string strConnectionString = "integrated security=SSPI;    data source=(local);persist security info=True;    initial catalog=AffilliatePOC";SqlConnection con = new SqlConnection(strConnectionString);	// First find out the quantity of product IDstring sql = "Select QUANTITY from Products where ProductID=@ID";SqlCommand sqlCheckQuantity = new SqlCommand(sql,con);sqlCheckQuantity.Parameters.Add("@ID",nProductID);con.Open();int x=Convert.ToInt16(sqlCheckQuantity.ExecuteScalar());// Then update the products to 1 less than thisint nNewQuantity = x-1;sql = "UPDATE Products Set Quantity=@NewVal WHERE ProductID=@ID";SqlCommand sqlUpdateQuantity = new SqlCommand(sql,con);sqlUpdateQuantity.Parameters.Add("@ID",nProductID);sqlUpdateQuantity.Parameters.Add("@NewVal",nNewQuantity);sqlUpdateQuantity.ExecuteNonQuery();// Then insert a new record of this purchase for this affiliatesql = "INSERT INTO TRANS (ProductID, AffiliateID, Quantity) Values(@ProductID, @AffiliateID, 1)";SqlCommand sqlInsertTrans = new SqlCommand(sql,con);sqlInsertTrans.Parameters.Add("@ProductID", nProductID);sqlInsertTrans.Parameters.Add("@AffiliateID", nAffiliateID);sqlInsertTrans.ExecuteNonQuery();con.Close();

In a real world scenario you would then construct a shipping order that is passed to your shipping system, and the shipping system would do the reduction at fulfillment. For completeness, I’ve included it in the method above.

Next Steps
This article has gone over the skeletal details for what it would take to construct an API that wraps the systems of a simple e-commerce store with a Web service, and then exposes that Web service as an API for consumption by affiliates. It also gave a brief example of how an affiliate could construct a branded Web site that consumes your API. To make it a production system, you would need to work from this infrastructure, adding code and functionality that consider how your internal systems work.

Perhaps most importantly you would need to consider how to secure your API against spoofing or tampering. The nice thing about doing it as a Web service is that there are already a number of Web services standards to help you. Steps that you can take are:

  • Obtaining a certificate to publish your Web service on https instead of http.
  • Using WS-Security and WSE 2.0 to encrypt your SOAP messages.
  • Using XML Signature and providing an API to validate that your SOAP messages are coming from a valid affiliate.

In this article I gave you a whirlwind tour for constructing a multi-tier system with a Web services-based API that serves as the back end for an open affiliate sales program. While the sample application takes a number of short cuts for the sake of demonstration, it wouldn’t take much to enhance it. In fact, extending this API could be a fun and fascinating process, and one that will pay off in both higher sales and more exposure of your goods and services.

Regardless of how you choose to extend this framework, it is a good demonstration of the issues that you will face when developing multi-tier, loosely coupled systems. With a bit of effort, you can turn your online store into the next Amazon, Paypal or eBay with an army of affiliates doing your marketing for you!

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist