Skip to content

SiftScience/sift-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sift-dotnet

Nuget (with prereleases)

The official Sift .NET client, supporting .NET Standard 2.0+

Documentation

Initialization

// You can also pass in your own HttpClient implementation as the second parameter.
// Dispose() will dispose of the HttpClient instance.
var sift = new Client("REST_API_KEY");

Reserved Events

// Construct reserved events with known fields for example Create Order
var createOrder = new CreateOrder
{
    user_id = "gary",
    order_id = "oid",
    amount = 1000000,
    currency_code = "USD",
    billing_address = new Address {
        name = "gary",
        city = "san francisco"
    },
    app = new App {
        app_name = "my app",
        app_version = "1.0"
    },
    items = new ObservableCollection<Item>() { new Item{sku="abc"}, new Item{sku="abc"} }
};

// Augment with custom fields
createOrder.AddField("foo", "bar");

try
{
    EventResponse res = sift.SendAsync(new EventRequest
    {
        Event = createOrder,
        ReturnScore = true,
        AbuseTypes = new List<string>() { "payment_abuse", "account_takeover" }
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

// Construct reserved events with known fields
var updatePassword = new UpdatePassword
        {
            user_id = "billy_jones_301",
            reason = "$forced_reset",
            status = "$success",
            session_id = "gigtleqddo84l8cm15qe4il",
            ip = "128.148.1.135",
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            },
            brand_name = "sift",
            site_country = "US",
            site_domain = "sift.com",
            user_email = "[email protected]",
            verification_phone_number = "+123456789012"
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = updatePassword
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }

// Construct reserved events with known fields AdditemtocartEvent
var addItemToCart = new AddItemToCart
        {
            user_id = "billy_jones_301",
            session_id = "gigtleqddo84l8cm15qe4il",
            item = new Item()
            {
                item_id = "B004834GQO",
                product_title = "The Slanket Blanket-Texas Tea",
                price = 39990000,
                currency_code = "USD",
                upc = "6786211451001",
                sku = "004834GQ",
                isbn = "0446576220",
                brand = "Slanket",
                manufacturer = "Slanket",
                category = "Blankets & Throws",
                tags = new ObservableCollection<string>() { "Awesome", "Wintertime specials" },
                color = "Texas Tea",
                quantity = 16,
            },
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            },
            brand_name = "sift",
            site_country = "US",
            site_domain = "sift.com",
            user_email = "[email protected]",
            verification_phone_number = "+123456789012"
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = addItemToCart
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }
// Construct reserved events with known fields AddPromotion
var addPromotion = new AddPromotion
        {
            user_id = "billy_jones_301",
            session_id = "gigtleqddo84l8cm15qe4il",
            promotions = new ObservableCollection<Promotion>()
            {
                new Promotion()
                {
                      promotion_id = "NewCustomerReferral2016",
                      status = "$success",
                      failure_reason = "$already_used",
                      description =   "$5 off your first 5 rides",
                      referrer_user_id = "elon-m93903",
                      discount = new Discount()
                      {
                              percentage_off = 0.2,
                              amount = 5000000,
                              currency_code = "USD",
                              minimum_purchase_amount = 50000000
                      },
                      credit_point = new CreditPoint()
                      {
                              amount = 5000,
                              credit_point_type = "character xp points"
                      }
                },
                new Promotion()
                {
                    promotion_id = "NewCustomerReferral2016"
                }
            },
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            },
            brand_name = "sift",
            site_country = "US",
            site_domain = "sift.com",
            user_email = "[email protected]",
            verification_phone_number = "+123456789012"
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = addPromotion
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }

// Construct reserved events with known fields ContentStatus
var contentStatus = new ContentStatus
        {
            user_id = "billy_jones_301",
            session_id = "gigtleqddo84l8cm15qe4il",
            content_id = "9671500641",
            status = "$paused",
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            },
            brand_name = "sift",
            site_country = "US",
            site_domain = "sift.com",
            user_email = "[email protected]",
            verification_phone_number = "+123456789012"
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = contentStatus
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }

// Construct reserved events with known fields FlagContent
var flagContent = new FlagContent
        {
            user_id = "billy_jones_301",
            session_id = "gigtleqddo84l8cm15qe4il",
            content_id = "9671500641",
            flagged_by = "jamieli89",
            reason = "$toxic",
            user_email = "[email protected]",
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            },
            verification_phone_number = "+123456789012"
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = flagContent
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }

// Construct reserved events with known fields RemoveItemFromCart
var removeItemFromCart = new RemoveItemFromCart
        {
            user_id = "billy_jones_301",
            session_id = "gigtleqddo84l8cm15qe4il",
            item = new Item()
            {
                item_id = "B004834GQO",
                product_title = "The Slanket Blanket-Texas Tea",
                price = 39990000,
                currency_code = "USD",
                upc = "6786211451001",
                sku = "004834GQ",
                isbn = "0446576220",
                brand = "Slanket",
                manufacturer = "Slanket",
                category = "Blankets & Throws",
                tags = new ObservableCollection<string>() { "Awesome", "Wintertime specials" },
                color = "Texas Tea",
                quantity = 2
            },
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            },
            brand_name = "sift",
            site_country = "US",
            site_domain = "sift.com",
            user_email = "[email protected]",
            verification_phone_number = "+123456789012"
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = removeItemFromCart
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }

// Construct reserved events with known fields CreateOrder
var createOrder = new CreateOrder
        {
            user_id = "billy_jones_301",
            session_id = "gigtleqddo84l8cm15qe4il",
            order_id = "ORDER-28168441",
            user_email = "[email protected]",
            verification_phone_number = "+123456789012",
            amount = 115940000,
            currency_code = "USD",
            billing_address = new Address()
            {
                name = "Bill Jones",
                phone = "1-415-555-6041",
                address_1 = "2100 Main Street",
                address_2 = "Apt 3B",
                city = "New London",
                region = "New Hampshire",
                country = "US",
                zipcode = "03257"
            },
            payment_methods = new ObservableCollection<PaymentMethod>()
            {
                new PaymentMethod()
                {
                    payment_type = "$credit_card",
                    payment_gateway = "$braintree",
                    card_bin = "542486",
                    card_last4 = "4444"
                },
                new PaymentMethod()
                {
                    payment_type = "$credit_card"
                }

            },
            ordered_from = new OrderedFrom()
            {
                store_id = "123",
                store_address = new Address()
                {
                    name = "Bill Jones",
                    phone = "1-415-555-6040",
                    address_1 = "2100 Main Street",
                    address_2 = "Apt 3B",
                    city = "New London",
                    region = "New Hampshire",
                    country = "US",
                    zipcode = "03257"
                }

            },
            brand_name = "sift",
            site_domain = "sift.com",
            site_country = "US",
            shipping_address = new Address()
            {
                name = "Bill Jones",
                phone = "1-415-555-6041",
                address_1 = "2100 Main Street",
                address_2 = "Apt 3B",
                city = "New London",
                region = "New Hampshire",
                country = "US",
                zipcode = "03257"
            },
            expedited_shipping = true,
            shipping_method = "$physical",
            shipping_carrier = "UPS",
            shipping_tracking_numbers = new ObservableCollection<string>() { "1Z204E380338943508", "1Z204E380338943509" },
            items = new ObservableCollection<Item>()
            {
                new Item()
                {
                    item_id = "12344321",
                    product_title = "Microwavable Kettle Corn: Original Flavor",
                    price = 4990000,
                    currency_code = "USD",
                    upc = "097564307560",
                    sku = "03586005",
                    isbn = "0446576220",
                    brand = "Peters Kettle Corn",
                    manufacturer = "Peters Kettle Corn",
                    category = "Food and Grocery",
                    tags = new ObservableCollection<string>() { "Popcorn", "Snacks", "On Sale" },
                    color = "Texas Tea",
                    quantity = 4
                },
                new Item()
                {
                    item_id = "12344321"
                }
            },
            seller_user_id = "slinkys_emporium",
            promotions = new ObservableCollection<Promotion>()
            {
                new Promotion()
                {
                    promotion_id = "FirstTimeBuyer",
                    status = "$success",
                    description = "$5 off",
                    discount = new Discount()
                    {
                        amount = 5000000,
                        currency_code = "USD",
                        minimum_purchase_amount = 25000000
                    }
                }
            },
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            }
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = createOrder
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }

// Construct reserved events with known fields UpdateOrder
var updateOrder = new UpdateOrder
        {
            user_id = "billy_jones_301",
            session_id = "gigtleqddo84l8cm15qe4il",
            order_id = "ORDER-28168441",
            user_email = "[email protected]",
            verification_phone_number = "+123456789012",
            amount = 115940000,
            currency_code = "USD",
            billing_address = new Address()
            {
                name = "Bill Jones",
                phone = "1-415-555-6041",
                address_1 = "2100 Main Street",
                address_2 = "Apt 3B",
                city = "New London",
                region = "New Hampshire",
                country = "US",
                zipcode = "03257"
            },
            payment_methods = new ObservableCollection<PaymentMethod>()
            {
                new PaymentMethod()
                {
                    payment_type = "$credit_card",
                    payment_gateway = "$braintree",
                    card_bin = "542486",
                    card_last4 = "4444"
                },
                new PaymentMethod()
                {
                    payment_type = "$credit_card"
                }

            },
            ordered_from = new OrderedFrom()
            {
                store_id = "123",
                store_address = new Address()
                {
                    name = "Bill Jones",
                    phone = "1-415-555-6040",
                    address_1 = "2100 Main Street",
                    address_2 = "Apt 3B",
                    city = "New London",
                    region = "New Hampshire",
                    country = "US",
                    zipcode = "03257"
                }

            },
            brand_name = "sift",
            site_domain = "sift.com",
            site_country = "US",
            shipping_address = new Address()
            {
                name = "Bill Jones",
                phone = "1-415-555-6041",
                address_1 = "2100 Main Street",
                address_2 = "Apt 3B",
                city = "New London",
                region = "New Hampshire",
                country = "US",
                zipcode = "03257"
            },
            expedited_shipping = true,
            shipping_method = "$physical",
            shipping_carrier = "UPS",
            shipping_tracking_numbers = new ObservableCollection<string>() { "1Z204E380338943508", "1Z204E380338943509" },
            items = new ObservableCollection<Item>()
            {
                new Item()
                {
                    item_id = "12344321",
                    product_title = "Microwavable Kettle Corn: Original Flavor",
                    price = 4990000,
                    currency_code = "USD",
                    upc = "097564307560",
                    sku = "03586005",
                    isbn = "0446576220",
                    brand = "Peters Kettle Corn",
                    manufacturer = "Peters Kettle Corn",
                    category = "Food and Grocery",
                    tags = new ObservableCollection<string>() { "Popcorn", "Snacks", "On Sale" },
                    color = "Texas Tea",
                    quantity = 4
                },
                new Item()
                {
                    item_id = "12344321"
                }
            },
            seller_user_id = "slinkys_emporium",
            promotions = new ObservableCollection<Promotion>()
            {
                new Promotion()
                {
                    promotion_id = "FirstTimeBuyer",
                    status = "$success",
                    description = "$5 off",
                    discount = new Discount()
                    {
                        amount = 5000000,
                        currency_code = "USD",
                        minimum_purchase_amount = 25000000
                    }
                }
            },
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            },
            merchant_profile = new MerchantProfile()
            {
                merchant_id = "AX527123",
                merchant_category_code = "1234",
                merchant_name = "Dream Company",
                merchant_address = new Address()
                {
                    phone = "1-415-555-6040",
                    address_1 = "2100 Main Street",
                    address_2 = "Apt 3B",
                    city = "New London",
                    region = "New Hampshire",
                    country = "US",
                    zipcode = "03257"

                }
            }
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = updateOrder
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }

// Construct reserved events with known fields CreateContentComment
var createContent = new CreateContent
        {
            user_id = "fyw3989sjpqr71",
            content_id = "comment-23412",
            session_id = "a234ksjfgn435sfg",
            status = "$active",
            ip = "255.255.255.0",
            comment = new Comment()
            {
                body = "Congrats on the new role!",
                contact_email = "[email protected]",
                parent_comment_id = "comment-23407",
                root_content_id = "listing-12923213",
                images = new ObservableCollection<Image>()
                {
                    new Image()
                    {
                        md5_hash = "0cc175b9c0f1b6a831c399e269772661",
                        link = "https://www.domain.com/file.png",
                        description =   "An old picture"
                    },
                    new Image()
                    {
                        md5_hash = "0cc175b9c0f1b6a831c399e269772661"
                    }
                }
            },
            browser = new Browser
            {
                user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                accept_language = "en-US",
                content_language = "en-GB"
            },
        };

        EventRequest eventRequest = new EventRequest()
        {
            Event = createContent
        };
        try
        {
            EventResponse res = sift.SendAsync(eventRequest).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }
    }


// Construct reserved events with known fields for example Verification
var verification = new Verification
{
    user_id = "billy_jones_301",
    session_id = "wwqr",
    status = "$pending",
    browser = new Browser
    {
        user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
        accept_language = "en-GB",
        content_language = "en-US"
    },
    verified_event = "$login",
    verified_entity_id = "123",
    verification_type = "$sms",
    verified_value = "14155551212",
    reason = "$user_setting",
    brand_name = "xyz",
    site_country = "AU",
    site_domain = "somehost.example.com"
};
var sift = new Client("API_KEY");


EventRequest eventRequest = new EventRequest
{
    Event = verification
};

try
{
    EventResponse res = sift.SendAsync(eventRequest).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

IncludeScorePercentile in EventRequest

  EventRequest eventRequest = new EventRequest
  {
    Event = transaction,
    AbuseTypes = { "legacy", "payment_abuse" },
    IncludeScorePercentile = true, // this will include the relevant parameters in the url query string to get the SCORE_PERCENTILE
    ReturnScore = true
  };

Custom Events

// Construct custom events with required fields
var makeCall = new CustomEvent
{
    type = "make_call",
    user_id = "gary"
};

// Augment with custom fields
makeCall.AddFields(new Dictionary<string, object>
{
    ["foo"] = "bar",
    ["payment_status"] = "$success"
});

try
{
    EventResponse res = sift.SendAsync(new EventRequest
    {
        Event = makeCall
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

// Construct custom events with required fields
var getScoreResponse = new ScoreResponse
        {

            Status = 0,
            ErrorMessage = "OK",
            UserId = "billy_jones_301",
            Scores = new Dictionary<string, ScoreResponse.ScoreJson>()
            {
                {
                   "score",
                    new ScoreResponse.ScoreJson()
                        {Score=6, Time=84710383103092309,
                        Reasons= new List<ScoreResponse.ReasonJson>()
                        {
                            new ScoreResponse.ReasonJson()
                            {Name="UsersPerDevice", Value=4,
                                Details=new Dictionary<string, object>()
                                {
                                    {
                                        "users",
                                        new List<string>()
                                        {
                                            "a","b","c","d"
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            },
            EntityType = "user",
            EntityId = "Id",
            LatestDecisions = new Dictionary<string, ScoreResponse.DecisionJson>()
            {
                {
                    "Id", new ScoreResponse.DecisionJson()
                    {
                        id = "user_looks_bad_payment_abuse",
                        type = "block",
                        source = "AUTOMATED_RULE",
                        Time  = 1352201880,
                        description ="Bad Fraudster"
                    }
                }
            },
            LatestLabels = new Dictionary<string, ScoreResponse.LabelJson>()
            {
                {
                    "is_fraud", new ScoreResponse.LabelJson()
                    {
                        is_fraud = true,
                        Time = 1352201880,
                        Description = "received a chargeback"
                    }
                }
            },
        };

Decisions

// Apply Decision
try
{
    ApplyDecisionResponse response = sift.SendAsync(new ApplyUserDecisionRequest
    {
        AccountId = "ACCOUNT_ID",
        UserId = "gary",
        DecisionId = "DECISION_ID",
        Source = "AUTOMATED_RULE"
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}



// Get Decision Status
try
{
    GetDecisionStatusResponse response = sift.SendAsync(new GetDecisionStatusRequest
    {
        AccountId = "ACCOUNT_ID",
        UserId = "gary"
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

// Get Decisions
try
{
    GetDecisionsResponse response = sift.SendAsync(new GetDecisionsRequest
    {
        AccountId = "ACCOUNT_ID"
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

Workflows

// Workflow Status
try
{
    WorkflowStatusResponse response = sift.SendAsync(new WorkflowStatusRequest
    {
        AccountId = "ACCOUNT_ID",
        WorkflowRunId = "WORKFLOW_RUN_ID"
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

Scores

// Get score
try
{
    ScoreResponse res = sift.SendAsync(new ScoreRequest
    {
        UserId = "gary"
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

// Rescore
try
{
    ScoreResponse res = sift.SendAsync(new RescoreRequest
    {
        UserId = "gary"
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

Percentile

 [JsonProperty("percentiles")]
public Dictionary<string, decimal> Percentiles { get; set; }

It's a collection of type dictionary with string Key and a decimal Value

Labels

// Label
try
{
    SiftResponse response = sift.SendAsync(new LabelRequest
    {
        UserId = "gary",
        IsBad = true,
        AbuseType = "payment_abuse"
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

// Unlabel
try
{
    SiftResponse response = sift.SendAsync(new UnlabelRequest
    {
        UserId = "gary",
        AbuseType = "payment_abuse"
    }).Result;
}
catch (AggregateException ae)
{
    // Handle InnerException
}

Verification

// send
try
        {
            VerificationSendResponse res = sift.SendAsync(new VerificationSendRequest
                {
                    UserId = "USER_ID",
                    BrandName = "MyTopBrand",
                    VerificationType = "$email",
                    SendTo = "SEND_TO",
                    Language = "en",
                    SiteCountry = "IN",
                    Event = new VerificationSendEvent()
                    {
                        Browser = new Browser()
                        {
                            user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                            content_language = "en-US",
                            accept_language = "en-GB",
                        },
                        IP = "192.168.1.1",
                        Reason = "$automated_rule",
                        SessionId = "SOME_SESSION_ID",
                        VerifiedEvent = "$login",
                        VerifiedEntityId = "SOME_SESSION_ID",
                    }
                }
            ).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }

// resend
    try
        {
            VerificationReSendResponse res = sift.SendAsync(new VerificationReSendRequest
                {
                    UserId = "USER_ID",
                    VerifiedEntityId = "SOME_SESSION_ID",
                    VerifiedEvent = "$login"

                }
            ).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }

// check
     try
        {
            VerificationCheckResponse res = sift.SendAsync(new VerificationCheckRequest
                {
                    Code = 147222,
                    UserId = "USER_ID",
                    VerifiedEvent = "$login",
                    VerifiedEntityId = "SOME_SESSION_ID"

                }
            ).Result;
        }
        catch (AggregateException ae)
        {
            // Handle InnerException
        }