# Examples

# dotnet

Below is a complete example of the SIW API integration in C#. All other API follow the same pattern.

using System;
using System.Collections.Generic;
using System.Text;
using Ingrid.SIW;
using Ingrid.SIW.Models;
using Microsoft.Rest;

namespace Test {
  class Program {
    static void Main(string[] args) {
      var hostname = "https://api-stage.ingrid.com/v1/siw";
      var apiToken = "your-ingrid-api-token";
      var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(apiToken));
      var credentials = new TokenCredentials(encoded.ToString());
      var client = new SiwClient(new Uri(hostname, UriKind.Absolute), credentials);

      var createRequest = new CreateSessionRequest {
          PurchaseCountry = "SE",
          PurchaseCurrency = "SEK",
          Locale = "sv-SE",
          ExternalId = "order-134",
          SearchAddress = new Address {
            PostalCode = "11735",
            Country = "SE"
          },
          Cart = new Cart{
            CartId = "cart-123",
            Currency = "SEK",
            TotalValue = 8900,
            Items = new List<CartItem>() {
                new CartItem {
                  Sku = "item-123",
                  Name = "Thing",
                  OutOfStock = true,
                },
                new CartItem {
                  Sku = "item-234",
                  Name = "Widget",
                  Attributes = new string[] {"express", "food", "rx"},
                }
            }
          }
      };

      try {
        var createResponse = client.CreateSession(createRequest);

        Console.WriteLine("Created session with id {0}", createResponse.Session.Id);

        var updateRequest = new UpdateSessionRequest {
            Id = createResponse.Session.Id,
            Cart = new Cart {
              CartId = "cart-123",
              Currency = "SEK",
              TotalValue = 2900,
              Items = new List<CartItem>() {
                  new CartItem {
                    Sku = "item-234",
                    Name = "Ibuprofen 400 mg",
                    Attributes = new string[] {"express", "medicine"},
                  }
              }
            }
        };

        var updateResp = client.UpdateSession(updateRequest);
        Console.WriteLine("Updated session with id {0}", updateResp.Session.Id);

        var completeRequest = new CompleteSession{
          Id = createResponse.Session.Id,
          // Customer information is required in the session complete call
          Customer = new CustomerInfo {
            Phone = "+46736355567",
            Email = "erik@example.com",
            Address = new Address{
              Name = "Erik Johansson",
              AddressLines = new string[] {"Folkskolegatan 4"},
              PostalCode = "11735",
              City = "Stockholm",
              Country = "SE"
            },
          }
        };

        var completeResp = client.CompleteSession(completeRequest);

        Console.WriteLine("Completed session with id {0}\n", completeResp.Session.Id);
     }
     catch (HttpOperationException ex) {
        Console.WriteLine(ex.Response.Content);
     }
    }
  }
}

Last Updated: 12/2/2020, 3:19:01 PM