Skip to content

JS Plugin

The Samco Publisher JavaScript plugin allows you to integrate one-click trade buttons into your webpage. Operating similar to a combination of a shopping basket and a payment gateway, it initiates a new window on your webpage, assisting users through the trading process before returning them to your page.

Using the JavaScript plugin, you have the flexibility to dynamically add one or more stocks to the basket, with a maximum limit of 10. Alternatively, you can embed straightforward static buttons using plain HTML for a simpler approach.

Create your buttons

js
<script src="https://web2.samco.in/publisher.js?v=1"></script>

To integrate Samco Publisher into your webpage, simply insert the above script tag at the end of your webpage, right before the closing </body> tag. You only need to include this script once, and it will enable the rendering of any number of buttons on the page.

To render branded Samco buttons that initiate a trade with a single click, you can use the custom HTML5 tag <samco-button>. These buttons function similarly to social media buttons and can be included as many times as needed on a page.

Branded HTML5 buttons

html
<!--  Here's a simple HTML link that would initiate a market buy of the IDEA stock:  -->
<samco-button
  href="#"
  data-samco="your-api-key"
  data-exchange="NSE"
  data-trading_symbol="IDEA"
  data-transaction_type="BUY"
  data-quantity="1"
  data-order_type="MARKET"
  >Buy IDEA stock</samco-button>

Custom HTML5 buttons

You can leverage HTML5 data attributes on any HTML element to transform it into a trade button, which can then be activated with a single click. The examples below demonstrate how a link and a button can be converted into trade buttons.

html
<!--  Here's a simple HTML link that would initiate a MARKET BUY of the IDEA stock:  -->
<a
  href="#"
  data-samco="your-api-key"
  data-exchange="NSE"
  data-transaction_type="BUY"
  data-trading_symbol="IDEA"
  data-order_type="MARKET"
  data-quantity="1"
  >Buy IDEA stock</a>


<!--  Here's a simple HTML button that would initiate a LIMIT SELL of the TCS stock: //-->
<button
  data-samco="your-api-key"
  data-exchange="NSE"
  data-transaction_type="SELL"
  data-trading_symbol="TCS"
  data-order_type="LIMIT"
  data-quantity="1"
  data-price="4095"
>
  Sell TCS
</button>

<!--  Here's a simple HTML button that would initiate a CO order of the RELIANCE stock: //-->
<button
  data-samco="your-api-key"
  data-exchange="NSE"
  data-transaction_type="BUY"
  data-trading_symbol="RELIANCE"
  data-order_type="LIMIT"
  data-quantity="1"
  data-product="CO"
  data-price="2860"
  data-trigger_price="2840"
>
  Buy RELIANCE (Cover Order)
</button>

Generating dynamic buttons with Javascript

html
<!-- A Samco button will be generated within the specified container. //-->
<p id="default-button"></p>

<!-- The basket will be linked to the 'onClick' event of this element. //-->
<button id="custom-button">Buy the basket</button>

<!-- Include the plugin //-->
<script src="https://web2.samco.in/js/publisher/publisher.js?v=1"></script>

<script>
  // Only run your custom code once SamcoPublisher has fully initialised.
  // Use SamcoPublisher.ready() to achieve this.
  SamcoPublisher.ready(function () {
    // Initialize a new Publisher instance.
    // You can initialize multiple instances if you need.
    var publisher = new SamcoPublisher("your-api-key");

    // Add a stock to the basket
    publisher.add({
      exchange: "NSE",
      transaction_type: "BUY",
      trading_symbol: "IDEA",
      order_type: "MARKET",
      quantity: 1,
    });

    // Add another stock (Intra day)
    publisher.add({
      exchange: "NSE",
      transaction_type: "SELL",
      trading_symbol: "TCS",
      order_type: "LIMIT",
      quantity: 1,
      price: 4080,
      product: "MIS"
    });

    //Add a Cover Order
    publisher.add({
      exchange: "NSE",
      transaction_type: "BUY",
      trading_symbol: "RELIANCE",
      order_type: "LIMIT",
      product: "CO",
      price: 2862.5,
      quantity: 1,
      stoploss: 2850
    });

    //Add a Bracket Order
    publisher.add({
      exchange: "NSE",
      transaction_type: "BUY",
      trading_symbol: "SBIN",
      order_type: "LIMIT",
      product: "BO",
      quantity: 1,
      squareoff : 680,
      price: 670,
      stoploss: 660,
      trailing_stoploss : 5
    });

    // Register an (optional) callback.
    publisher.finished(function (status) {
      alert("Finished. Status is " + status);
    });

    // Render the in-built button inside a given target
    publisher.renderButton("#default-button");

    // OR, link the basket to any existing element you want
    publisher.link("#custom-button");
  });
</script>

You have the option to create a basket of stocks and have the plugin render a Samco button that executes it. Alternatively, you can link the basket to your own button or any HTML element of your choice.

To ensure proper initialization of your custom SamcoPublisher calls, it's essential to execute them after the plugin's assets have loaded asynchronously. You can achieve this by utilizing the SamcoPublisher.ready() function. This function ensures that your custom calls are executed only when the plugin is fully loaded and ready to be utilized.


Parameters

NameTypeRequiredDescription
bodyobjectfalseThis parameter can include additional data as needed.
trading_symbolstringtrueFor Equity Cash symbols, use "symbol"; for F&O contracts, use "Trading Symbol". Both can be found in ScripMaster.csv.
exchangestringtrueThe exchange for placing the order. Valid values are BSE, NSE, NFO, MCX, CDS. Default is NSE if not provided.
transaction_typestringtrueSpecifies the transaction type. Options are BUY or SELL.
order_typestringtrueType of order. Can be MARKET (Market Order), LIMIT (Limit Order), SL (Stop Loss Limit), SL-M (Stop Loss Market).
quantitystringtrueThe quantity for which the order is being placed.
productstringfalseProduct type of the order. Options include CNC (Cash and Carry), NRML (Normal), MIS (Intraday), CO (Cover Order), BO (Bracket Order). Default is CNC.
pricestringtrueThe price at which the order will be placed.
trigger_pricestringfalseThe price at which the order should be triggered for SL or SL-M orders.
disclosed_quantitystringtrueIf provided, should be at least 10% of the actual order quantity.
validitystringtrueOrder validity. Can be DAY (valid for the trading day) or IOC (Immediate Or Cancel).
is_amostringtrueAfter Market Order flag. Use YES (1) or NO (0) to indicate if it is an after-market order.

Methods

MethodArgumentsDescription
bodyfalsenone
SamcoPublisher.ready()function()A secure wrapper for all API calls that asynchronously waits for all assets to load.
add()entryAdds a trading entry to the basket. The entry should be an object literal {} containing the parameters needed.
get()Returns an array of all added entries in the basket.
count()Returns the number of entries added to the basket.
setOption()key, valueSets the value for certain supported keys.
renderButton()element_selectorDisplays a branded Samco button within the specified target HTML element. The button initiates the transaction when clicked. The element_selector should be an HTML selector like #buy-button or .buttons.
link()element_selectorAssociates the basket with the designated HTML element. Clicking this element will initiate the transaction in a new window tab. The element_selector should be an HTML selector like #buy-button or .buttons.
html()Generates an HTML form with required hidden fields and the basket payload serialized. Append this form to the document body and submit it to initiate the transaction.
finished()function(status)Sets up a callback function that activates after the order placement process is completed. The status provides the result of the order process.