Validating a Spacekey in a blueprint

Hi

I am new to developing the confluence plugins. I have a space plugin that is creating new spaces from template.

I am trying to figure out how to use the java script to validate that the space key does not already exist.

Any help would be appreciated

Thanks

Easiest way is to call the rest API via AJAX and if result is empty, no space exist with this key.
Exemple :

function existProject(spaceKey) {
        AJS.$.ajax({
            url: AJS.contextPath() + "/rest/api/space?spaceKey=" + spaceKey,
            type: "GET",
            dataType: "json",
            success: function (response) {
                if (response.size > 0) {
                    //Manage error here
                } else {
                   //Manage success here
                }
            }
        });
    }

Thanks

That helps a lot.