> ## Documentation Index
> Fetch the complete documentation index at: https://help.cryptolens.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Go

> The documentation of the Go client SDK

<Note>
  **Note**, currently only the **activate** method from the Web API is supported.
</Note>

In order to get started with the library, start by

```shellscript theme={null}
go get github.com/Cryptolens/cryptolens-golang/cryptolens
```

Now we can use the library in our code. A working example of the following can be found in the `examples/example_activate` directory (the repo link: [https://github.com/Cryptolens/cryptolens-golang/tree/go\_mod](https://github.com/Cryptolens/cryptolens-golang/tree/go_mod))

We start by importing the library with:

```go theme={null}
import "github.com/Cryptolens/cryptolens-golang/cryptolens"
```

Now we can activate a license key as follows:

```go theme={null}
token := "WyI0NjUiLCJBWTBGTlQwZm9WV0FyVnZzMEV1Mm9LOHJmRDZ1SjF0Vk52WTU0VzB2Il0="
publicKey := "<RSAKeyValue><Modulus>khbyu3/vAEBHi339fTuo2nUaQgSTBj0jvpt5xnLTTF35FLkGI+5Z3wiKfnvQiCLf+5s4r8JB/Uic/i6/iNjPMILlFeE0N6XZ+2pkgwRkfMOcx6eoewypTPUoPpzuAINJxJRpHym3V6ZJZ1UfYvzRcQBD/lBeAYrvhpCwukQMkGushKsOS6U+d+2C9ZNeP+U+uwuv/xu8YBCBAgGb8YdNojcGzM4SbCtwvJ0fuOfmCWZvUoiumfE4x7rAhp1pa9OEbUe0a5HL+1v7+JLBgkNZ7Z2biiHaM6za7GjHCXU8rojatEQER+MpgDuQV3ZPx8RKRdiJgPnz9ApBHFYDHLDzDw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"

licenseKey, err := cryptolens.KeyActivate(token, cryptolens.KeyActivateArguments{
	ProductId:   3646,
	Key:         "MPDWY-PQAOW-FKSCH-SGAAU",
	MachineCode: "289jf2afs3",
})
if err != nil || !licenseKey.HasValidSignature(publicKey) {
	fmt.Println("License key activation failed!")
	return
}
```

In order to use the code above with your account on [cryptolens.io](http://cryptolens.io) you need to change the constants as follows:

1. The `token` need to be changed to a valid access token for your account. Access tokens can be created at [https://app.cryptolens.io/User/AccessToken/](https://app.cryptolens.io/User/AccessToken/). In order to be able to use the `KeyActivate()` function the token needs to have the `Activate` scope.
2. The correct value for `publicKey` for your account can be found when logged in on [Cryptolens.io](http://Cryptolens.io) from the menu in the top-right corner ("Hello !") and then *Security Settings*. Copy paste the value from the *Public key* field.
3. The `ProductId` can be found at the page for the corresponding product at [https://app.cryptolens.io/Product](https://app.cryptolens.io/Product).
4. The `Key` is the license key string, and would in most cases be entered by the user of the application in some application dependent manner.
5. The `MachineCode` is an optional argument allowing you to provide an identifier for which device the application is running on, or something with a similar purpuse.

Finally, additional properties of the license key can be checked if desired:

```go theme={null}
fmt.Printf("License key for product with id: %d\n", licenseKey.ProductId)

if time.Now().After(licenseKey.Expires) {
	fmt.Println("License key has expired")
	return
}

if licenseKey.F1 {
	fmt.Println("Welcome! Pro version enabled!")
} else {
	fmt.Println("Welcome!")
}
```

## Examples

<CodeGroup>
  ```go Key verification theme={null}
  package main

  import (
  	"fmt"
  	"github.com/Cryptolens/cryptolens-golang/cryptolens"
  	"time"
  )

  func main() {
  	token := "WyI0NjUiLCJBWTBGTlQwZm9WV0FyVnZzMEV1Mm9LOHJmRDZ1SjF0Vk52WTU0VzB2Il0="
  	publicKey := "<RSAKeyValue><Modulus>khbyu3/vAEBHi339fTuo2nUaQgSTBj0jvpt5xnLTTF35FLkGI+5Z3wiKfnvQiCLf+5s4r8JB/Uic/i6/iNjPMILlFeE0N6XZ+2pkgwRkfMOcx6eoewypTPUoPpzuAINJxJRpHym3V6ZJZ1UfYvzRcQBD/lBeAYrvhpCwukQMkGushKsOS6U+d+2C9ZNeP+U+uwuv/xu8YBCBAgGb8YdNojcGzM4SbCtwvJ0fuOfmCWZvUoiumfE4x7rAhp1pa9OEbUe0a5HL+1v7+JLBgkNZ7Z2biiHaM6za7GjHCXU8rojatEQER+MpgDuQV3ZPx8RKRdiJgPnz9ApBHFYDHLDzDw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"

  	licenseKey, err := cryptolens.KeyActivate(token, cryptolens.KeyActivateArguments{
  		ProductId:   3646,
  		Key:         "MPDWY-PQAOW-FKSCH-SGAAU",
  		MachineCode: "289jf2afs3",
  	})
  	if err != nil || !licenseKey.HasValidSignature(publicKey) {
  		fmt.Println("License key activation failed!")
  		return
  	}

  	fmt.Printf("License key for product with id: %d\n", licenseKey.ProductId)

  	if time.Now().After(licenseKey.Expires) {
  		fmt.Println("License key has expired")
  		return
  	}

  	if licenseKey.F1 {
  		fmt.Println("Welcome! Pro version enabled!")
  	} else {
  		fmt.Println("Welcome!")
  	}
  }
  ```

  ```go Offline verification theme={null}
  package main

  import (
  	"errors"
  	"fmt"
  	"github.com/Cryptolens/cryptolens-golang/cryptolens"
  	"io/ioutil"
  	"time"
  )

  func ActivateAndSaveLicenseKey() (string, error) {
  	token := "WyI0NjUiLCJBWTBGTlQwZm9WV0FyVnZzMEV1Mm9LOHJmRDZ1SjF0Vk52WTU0VzB2Il0="
  	publicKey := "<RSAKeyValue><Modulus>khbyu3/vAEBHi339fTuo2nUaQgSTBj0jvpt5xnLTTF35FLkGI+5Z3wiKfnvQiCLf+5s4r8JB/Uic/i6/iNjPMILlFeE0N6XZ+2pkgwRkfMOcx6eoewypTPUoPpzuAINJxJRpHym3V6ZJZ1UfYvzRcQBD/lBeAYrvhpCwukQMkGushKsOS6U+d+2C9ZNeP+U+uwuv/xu8YBCBAgGb8YdNojcGzM4SbCtwvJ0fuOfmCWZvUoiumfE4x7rAhp1pa9OEbUe0a5HL+1v7+JLBgkNZ7Z2biiHaM6za7GjHCXU8rojatEQER+MpgDuQV3ZPx8RKRdiJgPnz9ApBHFYDHLDzDw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"

  	licenseKey, err := cryptolens.KeyActivate(token, cryptolens.KeyActivateArguments{
  		ProductId:   3646,
  		Key:         "MPDWY-PQAOW-FKSCH-SGAAU",
  		MachineCode: "289jf2afs3",
  	})
  	if err != nil || !licenseKey.HasValidSignature(publicKey) {
  		return "", errors.New("Initial license key activation failed")
  	}

  	serialized, err := licenseKey.ToBytes()
  	if err != nil {
  		return "", err
  	}

  	f, err := ioutil.TempFile("", "cryptolens_example_offline_")
  	if err != nil {
  		return "", err
  	}
  	defer f.Close()

  	_, err = f.Write(serialized)
  	if err != nil {
  		return "", err
  	}

  	return f.Name(), nil
  }

  func main() {
  	filename, err := ActivateAndSaveLicenseKey()
  	if err != nil {
  		fmt.Println("Failed to activate or save license key")
  		return
  	}

  	fmt.Printf("License key saved to file %s\n\n", filename)

  	savedKeyBytes, err := ioutil.ReadFile(filename)
  	if err != nil {
  		fmt.Println("Failed to read saved license key")
  		return
  	}

  	licenseKey, err := cryptolens.KeyFromBytes(savedKeyBytes)
  	if err != nil || !licenseKey.HasValidSignature(publicKey) {
  		fmt.Println("Error in saved license key")
  		return
  	}

  	fmt.Printf("License key sucessfully loaded from file!\n")

  	if time.Now().After(licenseKey.Expires) {
  		fmt.Println("License key has expired")
  		return
  	}

  	if licenseKey.F1 {
  		fmt.Println("Welcome! Pro version enabled!")
  	} else {
  		fmt.Println("Welcome!")
  	}

  }
  ```
</CodeGroup>
