You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Access keys can be managed through our dashboard.
33
+
accessKey:="your-access-key"
34
34
35
-
Now you can query the API for information or send data. For example, if we want to request our balance information you'd do something like this:
35
+
// Create a client.
36
+
client:= messagebird.New(accessKey)
36
37
37
-
```go
38
-
// Request the balance information, returned as a Balance object.
38
+
// Request the balance information, returned as a balance.Balance object.
39
39
balance, err:= balance.Read(client)
40
40
if err != nil {
41
-
switcherrResp:= err.(type) {
42
-
case messagebird.ErrorResponse:
43
-
for_, mbError:=range errResp.Errors {
44
-
fmt.Printf("Error: %#v\n", mbError)
45
-
}
46
-
}
47
-
41
+
// Handle error.
48
42
return
49
43
}
50
44
51
-
fmt.Println(" payment :", balance.Payment)
52
-
fmt.Println(" type :", balance.Type)
53
-
fmt.Println(" amount :", balance.Amount)
45
+
// Display the results.
46
+
fmt.Println("Payment: ", balance.Payment)
47
+
fmt.Println("Type:", balance.Type)
48
+
fmt.Println("Amount:", balance.Amount)
54
49
```
55
50
56
51
This will give you something like:
57
-
```shell
52
+
53
+
```bash
58
54
$ go run example.go
59
-
payment : prepaid
60
-
type: credits
61
-
amount : 9
55
+
Payment: prepaid
56
+
Type: credits
57
+
Amount: 9
62
58
```
63
59
64
60
Please see the other examples for a complete overview of all the available API calls.
65
61
62
+
Errors
63
+
------
64
+
When something goes wrong, our APIs can return more than a single error. They are therefore returned by the client as "error responses" that contain a slice of errors.
65
+
66
+
It is important to notice that the Voice API returns errors with a format that slightly differs from other APIs.
67
+
For this reason, errors returned by the `voice` package are of type `voice.ErrorResponse`. It contains `voice.Error` structs. All other packages return `messagebird.ErrorResponse` structs that contain a slice of `messagebird.Error`.
68
+
69
+
An example of "simple" error handling is shown in the example above. Let's look how we can gain more in-depth insight in what exactly went wrong:
70
+
71
+
```go
72
+
import"github.com/messagebird/go-rest-api"
73
+
import"github.com/messagebird/go-rest-api/sms"
74
+
75
+
// ...
76
+
77
+
_, err:= sms.Read(client, "some-id")
78
+
if err != nil {
79
+
mbErr, ok:= err.(messagebird.ErrorResponse)
80
+
if !ok {
81
+
// A non-MessageBird error occurred (no connection, perhaps?)
0 commit comments