# Issue in creating signature in Go Lang

**URL:** <https://community.rapyd.net/t/issue-in-creating-signature-in-go-lang/1627>\
**Category:** ⚙️ API\
**Created:** [April 29, 2022, 5:51am UTC](https://community.rapyd.net/t/issue-in-creating-signature-in-go-lang/1627 "2022-04-29T05:51:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdul\_Waheed](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rapyd.net/abdul_waheed/32/1609_2.png) [@Abdul\_Waheed](https://community.rapyd.net/u/Abdul_Waheed)\
**Post date:** [April 29, 2022, 5:51am UTC](https://community.rapyd.net/t/issue-in-creating-signature-in-go-lang/1627/1 "2022-04-29T05:51:17Z")

</div>

Hi,

I am trying to access Rapyd API and having problem creating signature.

To Create signature:

```auto
func sign(secret string, data []byte) string {
	hash := hmac.New(sha256.New, []byte(secret))

	hash.Write(data)
	return hex.EncodeToString(hash.Sum(nil))
}

```

To create to\_sign data:

```auto
//http_method e.g "get"
//path e.g "v1/data/countries"
//body e.g ""
//sample return value in string "get/v1/data/countriesa3d88998fcc545b41651211157_ accesskey_secret"
func to_sign(http_method, path, body, access_key, secret string) []byte {
	//timestamp to string
	ts := fmt.Sprintf("%d", time.Now().Unix())

	//salt generation
	key := make([]byte, 6)
	rand.Read(key)
	salt := fmt.Sprintf("%x", key)

	http_method = strings.ToLower(http_method)

	data := []byte(http_method + path + salt + ts + access_key + secret + body)

	return data
}

```

Error returned  
`{"status":{"error_code":"UNAUTHENTICATED_API_CALL","status":"ERROR","message":"The API received a request, but the signature did not match. The request was rejected. Corrective action: (1) Remove all whitespace that is not inside a string. (2) Remove trailing zeroes and decimal points, or wrap numbers in a string.","response_code":"UNAUTHENTICATED_API_CALL","operation_id":"9306d5ea-8c39-4898-afaa-47fc06170aaa"}}`

Go Version 1.18

---

<div class="post-metadata">

**Author:** ![Community\_Team](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Community\_Team](https://community.rapyd.net/u/Community_Team)\
**Post date:** [April 29, 2022, 4:41pm UTC](https://community.rapyd.net/t/issue-in-creating-signature-in-go-lang/1627/2 "2022-04-29T16:41:16Z")

</div>

Hi @Abdul_Waheed,

From the error response, this may be due to a couple things:

1. All spaces and other whitespace outside of strings must be removed.
2. Numbers should be sent in strings, not as integers/numbers.

Here are some resources that may help:

- [Rapyd API Request Signatures and How to Calculate](https://community.rapyd.net/t/rapyd-api-request-signatures-and-how-to-calculate/1256)
- [Verify Webhook Signature? - #2 by Minh\_Nguy\_n\_Van](https://community.rapyd.net/t/verify-webhook-signature/1383/2)
- [How to verify Webhook Signature with PHP script](https://community.rapyd.net/t/how-to-verify-webhook-signature-with-php-script/1382)
- [https://youtu.be/yL5TK4JfP7s](https://youtu.be/yL5TK4JfP7s)

I would also take a look around your body and and how it may not be parsed correctly.

---

<div class="post-metadata">

**Author:** ![Abdul\_Waheed](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rapyd.net/abdul_waheed/32/1609_2.png) [@Abdul\_Waheed](https://community.rapyd.net/u/Abdul_Waheed)\
**Post date:** [May 3, 2022, 12:31am UTC](https://community.rapyd.net/t/issue-in-creating-signature-in-go-lang/1627/3 "2022-05-03T00:31:34Z")

</div>

Passing empty body as making only get request.

```auto
	request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%v%v", baseUrl, countryUrl), bytes.NewBufferString(""))
	request.Header.Add("access_key", access_key)
	request.Header.Add("Content-Type", "application/json")
	request.Header.Add("salt", salt)
	request.Header.Add("timestamp", ts)
	request.Header.Add("signature", signature)

```

Is there any sample code in Go, it would be very useful.

---

<div class="post-metadata">

**Author:** ![Abdul\_Waheed](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rapyd.net/abdul_waheed/32/1609_2.png) [@Abdul\_Waheed](https://community.rapyd.net/u/Abdul_Waheed)\
**Post date:** [May 3, 2022, 12:41am UTC](https://community.rapyd.net/t/issue-in-creating-signature-in-go-lang/1627/4 "2022-05-03T00:41:37Z")

</div>

Thanks for your help Kyle, just need to convert it base64 after converting value into hex and add “==” at the end as in Go we need to manage trailing bit on our own.

Updated sign method.

```auto
func sign(secret string, data []byte) string {
	hash := hmac.New(sha256.New, []byte(secret))

	hash.Write(data)
	return base64.RawStdEncoding.EncodeToString([]byte(hex.EncodeToString(hash.Sum(nil)))) +"=="
}

```
