Page 1 of 1

API Key Generation

PostPosted: Tue Apr 21, 2020 6:59 pm
by SBarnes
Does the web api have a route to generate/return a Debtor API key, we have a client who wants the ability to generate an api key for the logged in debtor within their web store where the customer would then be able to use the generated api key for a specific set of routes related to file export?

If the route doesn't exist could you provide some sample code for generating the key?

I wouldn't be considering this except the customer wants to do it and the web store and api will be running under SSL, I may also implement some sort of TFA to protect it as well.

Re: API Key Generation

PostPosted: Tue Apr 21, 2020 11:09 pm
by Mike.Sheen
SBarnes wrote:Does the web api have a route to generate/return a Debtor API key, we have a client who wants the ability to generate an api key for the logged in debtor within their web store where the customer would then be able to use the generated api key for a specific set of routes related to file export?

If the route doesn't exist could you provide some sample code for generating the key?

I wouldn't be considering this except the customer wants to do it and the web store and api will be running under SSL, I may also implement some sort of TFA to protect it as well.


No, we don't have a route to generate or return a Debtor API key. It would be fairly simple to do - the debtor business logic does have the ability to do that, so it would be a matter of creating plugin to add that route, which would just invoke the necessary method and serialising a response.

We can provide a sample plugin of how to add a route to generate a debtor API key, and retrieve a key - but we are a little pressed for time ATM, so you may need to be a bit patient.

Re: API Key Generation  Topic is solved

PostPosted: Wed Apr 22, 2020 8:00 am
by SBarnes
Mike.Sheen wrote:We can provide a sample plugin of how to add a route to generate a debtor API key, and retrieve a key - but we are a little pressed for time ATM, so you may need to be a bit patient.


Hi Mike,

As always thanks for the response, from that I was able to follow my nose and found that the following with modification should get the key created, given you are under the pump I think I can get this resolved from here.

Code: Select all
                   APIKey aPIKey = this.Manager.CollectionItemFactory.CreateCollectionItem<APIKey>();
                    aPIKey.Staff.Search(this, "");
                    this.Debtor.APIKeys.Add(aPIKey);


                    APIKey item = this.Debtor.APIKeys[str1];
                    item.KeyValue = item.GenerateNewKey();



In other words create the api key object, set the staff member as required, add it to the collection on the debtor which I am assuming will let it know the debtor needed at save, call generate new key and then save the debtor.

Putting it in a service for the api in a plugin should be fairly straight forward from there.