# Software Licensing API - Example using JavaScript

This document is a subset of the [Software Licensing API document](https://easydigitaldownloads.com/docs/software-licensing-api/) which lists all features available via the API. The following is example code showcasing how to implement those features with JavaScript.

If you&#039;re using jQuery, see our [jQuery example](https://easydigitaldownloads.com/docs/software-licensing-api-example-using-jquery/).

### Sample GET Request

```
const url = new URL( &#039;https://yoursite.com/&#039; ); // Site with Software Licensing activated.
const urlParams = new URLSearchParams( {
	edd_action: &#039;check_license&#039;,
	license: &#039;59cc77ea94a2d867069a9d96142a35b8&#039;, // License key
	item_id: &#039;356&#039;, // Product ID
	url: &#039;domain.com&#039; // Domain the request is coming from.
} );
url.search = urlParams.toString();
fetch( url.toString() )
	.then( response =&gt; {
		if ( response.ok ) {
			return response.json();
		} else {
			return Promise.reject( response );
		}
	} ).then( data =&gt; {
		// Software Licensing has a valid response to parse
		console.log( &#039;Successful response&#039;, data );
	} ).catch( error =&gt; {
		// Error handling.
		console.log( &#039;Error&#039;, error );
	} );
```

### Sample POST Request

```
const formData = new FormData();
formData.append( &#039;edd_action&#039;, &#039;check_license&#039; ); // Valid actions are activate_license, deactivate_license, get_version, check_license
formData.append( &#039;license&#039;, &#039;59cc77ea94a2d867069a9d96142a35b8&#039; ); // License key
formData.append( &#039;item_id&#039;, &#039;356&#039; ); // Product ID
formData.append( &#039;url&#039;, &#039;domain.com&#039; ); // If you disable URL checking you do not need this entry.

// Site with Software Licensing activated.
fetch( &#039;https://yoursite.com/&#039;, {
	method: &#039;POST&#039;,
	body: formData
} ).then( response =&gt; {
	if ( response.ok ) {
		return response.json();
	} else {
		return Promise.reject( response );
	}
} ).then( data =&gt; {
	// Software Licensing has a valid response to parse
	console.log( &#039;Successful response&#039;, data );
} ).catch( error =&gt; {
	// Error handling.
	console.log( &#039;Error&#039;, error );
} );
```

### Response

A response to the above queries would be a JSON object that would look something like this:

```
{
    &quot;success&quot;: true,
    &quot;license&quot;: &quot;valid&quot;,
    &quot;item_id&quot;: 12534,
    &quot;item_name&quot;: &quot;Your Product Name&quot;,
    &quot;expires&quot;: &quot;2022-02-26 23:59:59&quot;,
    &quot;payment_id&quot;: 123456,
    &quot;customer_name&quot;: &quot;Jane Doe&quot;,
    &quot;customer_email&quot;: &quot;jane@sample.com&quot;,
    &quot;price_id&quot;: 0,
}
```

---

### Sample request for older browsers

The above examples are written with modern browsers in mind. If you require IE support then you may prefer to use an XMLHttpRequest object:

```
// Handling a Software licensing request without jQuery in pure JavaScript. Support for older browsers.
var xhttp = new XMLHttpRequest();

// The url to the site running Easy Digital Downloads w/Software Licensing
var postUrl = &#039;https://yoursite.com/&#039;;

xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 &amp;&amp; xhttp.status == 200) {
        var slData = JSON.parse(xhttp.responseText);
        handleSoftwareLicensingResponse( slData );
    }   
}

var data = {
	edd_action: &#039;check_license&#039;, // Valid actions are activate_license, deactivate_license, get_version, check_license
	license: &#039;59cc77ea94a2d867069a9d96142a35b8&#039;, // License key.
	item_id: &#039;356&#039;, // Product ID
	url: &#039;domain.com&#039; // If you Disable URL Checking, you do not need this entry
};

xhttp.open(&quot;POST&quot;, postUrl, true);
xhttp.setRequestHeader(&quot;Content-type&quot;, &quot;application/x-www-form-urlencoded&quot;);
xhttp.setRequestHeader(&quot;Access-Control-Allow-Origin&quot;, &quot;https://local.dev&quot;);

var values = &#039;&#039;;
for (var key in data){
    values += key + &#039;=&#039; + data[ key ] + &#039;&amp;&#039;;
}
values = values.substring(0, values.length - 1);

xhttp.send(values);


function handleSoftwareLicensingResponse( slData ) {
    if ( slData.success == true ) {
        console.log(slData);
        // Software Licensing has a valid response to parse
    } else {
        // Invalid request was made to software licensing
    }
}
```