Skip to content

SAISUDHEERREDDY/taxjar.net

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TaxJar Sales Tax API for .NET / C#

Official .NET / C# client for SmartCalcs by TaxJar. For the API documentation, please visit http://developers.taxjar.com/api.

Getting Started

We recommend installing TaxJar.net via NuGet. Before authenticating, get your API key from TaxJar.

Use the NuGet package manager inside Visual Studio, Xamarin Studio, or run the following command in the Package Manager Console:

PM> Install-Package TaxJar

Package Dependencies

TaxJar.net comes with assemblies for .NET 4.5.2 and .NET Standard 2.0. It requires the following dependencies:

These packages are automatically included when installing via NuGet.

Authentication

To authenticate with our API, add a new AppSetting with your TaxJar API key to your project's Web.config / App.config file or directly supply the API key when instantiating the client:

Method A

<!-- Web.config / App.config -->
<appSettings>
...
  <add key="TaxjarApiKey" value="[Your TaxJar API Key]" />
...
</appSettings>
var client = new TaxjarApi(ConfigurationManager.AppSettings["TaxjarApiKey"]);

Note: This method requires System.Configuration.ConfigurationManager on .NET Framework 4.x. If you'd like to use this method on .NET Standard or Core, reference the NuGet package in your project.

Method B

var client = new TaxjarApi("[Your TaxJar API Key]");

Usage

List all tax categories

var categories = client.Categories();

// Async Method
var categories = await client.CategoriesAsync();

List tax rates for a location (by zip/postal code)

var rates = client.RatesForLocation("90002", new {
  city = "LOS ANGELES",
  country = "US"
});

// Async Method
var rates = await client.RatesForLocationAsync("90002", new {
  city = "LOS ANGELES",
  country = "US"
});

// Request Entity
var rateEntity = new Rate {
  City = "LOS ANGELES",
  Country = "US"
};

var rates = client.RatesForLocation("90002", rateEntity);

Calculate sales tax for an order

var tax = client.TaxForOrder(new {
  from_country = "US",
  from_zip = "07001",
  from_state = "NJ",
  to_country = "US",
  to_zip = "07446",
  to_state = "NJ",
  amount = 16.50,
  shipping = 1.50
});

// Async Method
var tax = await client.TaxForOrderAsync(new {
  from_country = "US",
  from_zip = "07001",
  from_state = "NJ",
  to_country = "US",
  to_zip = "07446",
  to_state = "NJ",
  amount = 16.50,
  shipping = 1.50
});

// Request Entity
var taxEntity = new Tax {
  FromCountry = "US",
  FromZip = "07001",
  FromState = "NJ",
  ToCountry = "US",
  ToZip = "07446",
  ToState = "NJ",
  Amount = 16.50,
  Shipping = 1.50
};

var tax = client.TaxForOrder(taxEntity);

List order transactions

var orders = client.ListOrders(new {
  from_transaction_date = "2015/05/01",
  to_transaction_date = "2015/05/31"
});

// Async Method
var orders = await client.ListOrdersAsync(new {
  from_transaction_date = "2015/05/01",
  to_transaction_date = "2015/05/31"
});

// Request Entity
var orderFilter = new OrderFilter {
  FromTransactionDate = "2015/05/01",
  ToTransactionDate = "2015/05/31"
};

var orders = client.ListOrders(orderFilter);

Show order transaction

var order = client.ShowOrder("123");

// Async Method
var order = await client.ShowOrderAsync("123");

Create order transaction

var order = client.CreateOrder(new {
  transaction_id = "123",
  transaction_date = "2015/05/04",
  to_country = "US",
  to_zip = "90002",
  to_city = "Los Angeles",
  to_street = "123 Palm Grove Ln",
  amount = 17,
  shipping = 2,
  sales_tax = 0.95,
  line_items = new[] {
    new {
      quantity = 1,
      product_identifier = "12-34243-0",
      description = "Heavy Widget",
      unit_price = 15,
      sales_tax = 0.95
    }
  }
});

// Async Method
var order = await client.CreateOrderAsync(new {
  transaction_id = "123",
  transaction_date = "2015/05/04",
  to_country = "US",
  to_zip = "90002",
  to_city = "Los Angeles",
  to_street = "123 Palm Grove Ln",
  amount = 17,
  shipping = 2,
  sales_tax = 0.95,
  line_items = new[] {
    new {
      quantity = 1,
      product_identifier = "12-34243-0",
      description = "Heavy Widget",
      unit_price = 15,
      sales_tax = 0.95
    }
  }
});

// Request Entity
var orderEntity = new Order {
  TransactionId = "123",
  TransactionDate = "2015/05/04",
  ToCountry = "US",
  ToZip = "90002",
  ToCity = "Los Angeles",
  ToStreet = "123 Palm Grove Ln",
  Amount = 17,
  Shipping = 2,
  SalesTax = 0.95,
  LineItems = new List<LineItem> {
    new LineItem {
      Quantity = 1,
      ProductIdentifier = "12-34243-0",
      Description = "Heavy Widget",
      UnitPrice = 15,
      SalesTax = 0.95
    }
  }
};

var order = client.CreateOrder(orderEntity);

Update order transaction

var order = client.UpdateOrder(new {
  transaction_id = "123",
  amount = 17,
  shipping = 2,
  line_items = new[] {
    new {
      quantity = 1,
      product_identifier = "12-34243-0",
      description = "Heavy Widget",
      unit_price = 15,
      discount = 0,
      sales_tax = 0.95
    }
  }
});

// Async Method
var order = await client.UpdateOrderAsync(new {
  transaction_id = "123",
  amount = 17,
  shipping = 2,
  line_items = new[] {
    new {
      quantity = 1,
      product_identifier = "12-34243-0",
      description = "Heavy Widget",
      unit_price = 15,
      discount = 0,
      sales_tax = 0.95
    }
  }
});

// Request Entity
var orderEntity = new Order {
  TransactionId = "123",
  Amount = 17,
  Shipping = 2,
  LineItems = new List<LineItem> {
    new LineItem {
      Quantity = 1,
      ProductIdentifier = "12-34243-0",
      Description = "Heavy Widget",
      UnitPrice = 15,
      Discount = 0,
      SalesTax = 0.95
    }
  }
};

var order = client.UpdateOrder(orderEntity);

Delete order transaction

var order = client.DeleteOrder("123");

// Async Method
var order = await client.DeleteOrderAsync("123");

List refund transactions

var refunds = client.ListRefunds(new {
  from_transaction_date = "2015/05/01",
  to_transaction_date = "2015/05/31"
});

// Async Method
var refunds = await client.ListRefundsAsync(new {
  from_transaction_date = "2015/05/01",
  to_transaction_date = "2015/05/31"
});

// Request Entity
var refundFilter = new RefundFilter {
  FromTransactionDate = "2015/05/01",
  ToTransactionDate = "2015/05/31"
};

var refunds = client.ListRefunds(refundFilter);

Show refund transaction

var refund = client.ShowRefund("321");

// Async Method
var refund = await client.ShowRefundAsync("321");

Create refund transaction

var refund = client.CreateRefund(new {
  transaction_id = "321",
  transaction_date = "2015/05/04",
  transaction_reference_id = "123",
  to_country = "US",
  to_zip = "90002",
  to_city = "Los Angeles",
  to_street = "123 Palm Grove Ln",
  amount = 17,
  shipping = 2,
  sales_tax = 0.95,
  line_items = new[] {
    new {
      quantity = 1,
      product_identifier = "12-34243-0",
      description = "Heavy Widget",
      unit_price = 15,
      sales_tax = 0.95
    }
  }
});

// Async Method
var refund = await client.CreateRefundAsync(new {
  transaction_id = "321",
  transaction_date = "2015/05/04",
  transaction_reference_id = "123",
  to_country = "US",
  to_zip = "90002",
  to_city = "Los Angeles",
  to_street = "123 Palm Grove Ln",
  amount = 17,
  shipping = 2,
  sales_tax = 0.95,
  line_items = new[] {
    new {
      quantity = 1,
      product_identifier = "12-34243-0",
      description = "Heavy Widget",
      unit_price = 15,
      sales_tax = 0.95
    }
  }
});

// Request Entity
var refundEntity = new Refund {
  TransactionId = "321",
  TransactionDate = "2015/05/04",
  TransactionReferenceId = "123",
  ToCountry = "US",
  ToZip = "90002",
  ToCity = "Los Angeles",
  ToStreet = "123 Palm Grove Ln",
  Amount = 17,
  Shipping = 2,
  SalesTax = 0.95,
  LineItems = new List<LineItem> {
    new LineItem {
      Quantity = 1,
      ProductIdentifier = "12-34243-0",
      Description = "Heavy Widget",
      UnitPrice = 15,
      SalesTax = 0.95
    }
  }
};

var refund = client.CreateRefund(refundEntity);

Update refund transaction

var refund = client.UpdateRefund(new {
  transaction_id = "321",
  amount = 17,
  shipping = 2,
  line_items = new[] {
    new {
      quantity = 1,
      product_identifier = "12-34243-0",
      description = "Heavy Widget",
      unit_price = 15,
      discount = 0,
      sales_tax = 0.95
    }
  }
});

// Async Method
var refund = await client.UpdateRefundAsync(new {
  transaction_id = "321",
  amount = 17,
  shipping = 2,
  line_items = new[] {
    new {
      quantity = 1,
      product_identifier = "12-34243-0",
      description = "Heavy Widget",
      unit_price = 15,
      discount = 0,
      sales_tax = 0.95
    }
  }
});

// Request Entity
var refundEntity = new Refund {
  TransactionId = "321",
  Amount = 17,
  Shipping = 2,
  LineItems = new List<LineItem> {
    new LineItem {
      Quantity = 1,
      ProductIdentifier = "12-34243-0",
      Description = "Heavy Widget",
      UnitPrice = 15,
      Discount = 0,
      SalesTax = 0.95
    }
  }
};

var refund = client.UpdateRefund(refundEntity);

Delete refund transaction

var refund = client.DeleteRefund("321");

// Async Method
var refund = await client.DeleteRefundAsync("321");

List customers

var customers = client.ListCustomers();

// Async Method
var customers = await client.ListCustomersAsync();

Show customer

var customer = client.ShowCustomer("123");

// Async Method
var customer = await client.ShowCustomerAsync("123");

Create customer

var customer = client.CreateCustomer(new {
  customer_id = "123",
  exemption_type = "wholesale",
  name = "Dunder Mifflin Paper Company",
  exempt_regions = new[] {
    new {
      country = "US",
      state = "FL"
    },
    new {
      country = "US",
      state = "PA"
    }
  },
  country = "US",
  state = "PA",
  zip = "18504",
  city = "Scranton",
  street = "1725 Slough Avenue"
});

// Async Method
var customer = await client.CreateCustomerAsync(new {
  customer_id = "123",
  exemption_type = "wholesale",
  name = "Dunder Mifflin Paper Company",
  exempt_regions = new[] {
    new {
      country = "US",
      state = "FL"
    },
    new {
      country = "US",
      state = "PA"
    }
  },
  country = "US",
  state = "PA",
  zip = "18504",
  city = "Scranton",
  street = "1725 Slough Avenue"
});

// Request Entity
var customerEntity = new Customer {
  CustomerId = "123",
  ExemptionType = "wholesale",
  Name = "Dunder Mifflin Paper Company",
  ExemptRegions = new List<ExemptRegion> {
    new ExemptRegion {
      Country = "US",
      State = "FL"
    },
    new ExemptRegion {
      Country = "US",
      State = "PA"
    }
  },
  Country = "US",
  State = "PA",
  Zip = "18504",
  City = "Scranton",
  Street = "1725 Slough Avenue"
};

var customer = client.CreateCustomer(customerEntity);

Update customer

var customer = client.UpdateCustomer(new {
  customer_id = "123",
  exemption_type = "wholesale",
  name = "Sterling Cooper",
  exempt_regions = new[] {
    new {
      country = "US",
      state = "NY"
    }
  },
  country = "US",
  state = "NY",
  zip = "10010",
  city = "New York",
  street = "405 Madison Ave"
});

// Async Method
var customer = await client.UpdateCustomerAsync(new {
  customer_id = "123",
  exemption_type = "wholesale",
  name = "Sterling Cooper",
  exempt_regions = new[] {
    new {
      country = "US",
      state = "NY"
    }
  },
  country = "US",
  state = "NY",
  zip = "10010",
  city = "New York",
  street = "405 Madison Ave"
});

// Request Entity
var customerEntity = new Customer {
  CustomerId = "123",
  ExemptionType = "wholesale",
  Name = "Sterling Cooper",
  ExemptRegions = new List<ExemptRegion> {
    new ExemptRegion {
      Country = "US",
      State = "NY"
    }
  },
  Country = "US",
  State = "NY",
  Zip = "10010",
  City = "New York",
  Street = "405 Madison Ave"
};

var customer = client.UpdateCustomer(customerEntity);

Delete customer

var customer = client.DeleteCustomer("123");

// Async Method
var customer = await client.DeleteCustomerAsync("123");

List nexus regions

var nexusRegions = client.NexusRegions();

// Async Method
var nexusRegions = await client.NexusRegionsAsync();

Validate a VAT number

var validation = client.ValidateVat(new {
  vat = "FR40303265045"
});

// Async Method
var validation = await client.ValidateVatAsync(new {
  vat = "FR40303265045"
});

// Request Entity
var vatEntity = new Validation {
  Vat = "FR40303265045"
};

var validation = client.ValidateVat(vatEntity);

Summarize tax rates for all regions

var summaryRates = client.SummaryRates();

// Async Method
var summaryRates = await client.SummaryRatesAsync();

Custom Options

You can pass additional options using SetApiConfig or when instantiating the client for the following:

Timeouts

// Custom timeout when instantiating the client
var client = new TaxjarApi("[Your TaxJar API Key]", new { apiUrl = "https://api.taxjar.com", timeout = 30 * 1000 });

// Custom timeout via `SetApiConfig`
client.SetApiConfig("timeout", 30 * 1000);

Sandbox Environment

You can easily configure the client to use the TaxJar Sandbox:

var client = new TaxjarApi("[Your TaxJar Sandbox API Key]", new { apiUrl = "https://api.sandbox.taxjar.com" });

For testing specific error response codes, pass the custom X-TJ-Expected-Response header:

client.SetApiConfig("headers", new Dictionary<string, string> {
  { "X-TJ-Expected-Response", "422" }
});

Error Handling

When invalid data is sent to TaxJar or we encounter an error, we’ll throw a TaxjarException with the HTTP status code and error message. To catch these exceptions, refer to the example below. Click here for a list of common error response classes.

using Taxjar;
var client = new TaxjarApi();

try
{
  // Invalid request
  var order = client.CreateOrder(new {
    transaction_date = "2015/05/04",
    to_country = "US",
    to_state = "CA",
    to_zip = "90002",
    amount = 17.45,
    shipping = 1.5,
    sales_tax = 0.95
  });
}
catch(TaxjarException e)
{
  // 406 Not Acceptable – transaction_id is missing
  e.TaxjarError.Error;
  e.TaxjarError.Detail;
  e.TaxjarError.StatusCode;
}

Tests

We use NUnit and WireMock.Net for testing. Before running the specs, create a .env file inside the Taxjar.Tests directory with your sandbox API key:

TAXJAR_API_KEY=YOUR_TAXJAR_SANDBOX_API_KEY

More Information

More information can be found at TaxJar Developers.

License

TaxJar.net is released under the MIT License.

Support

Bug reports and feature requests should be filed on the GitHub issue tracking page.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new pull request

Packages

No packages published

Languages

  • C# 100.0%