Home >Backend Development >Golang >Trying Twilio Whatsappp api call but getting error 20422
I encountered an error while using twilio Whatsapp api.
The following is the code:
package main import ( "fmt" "github.com/twilio/twilio-go" api "github.com/twilio/twilio-go/rest/api/v2010" ) func main() { clientParameter := twilio.ClientParams{} clientParameter.Username = "AC***********************ba" clientParameter.Password = "ce************************27" clientParameter.AccountSid = "AC************************ba" client := twilio.NewRestClientWithParams(clientParameter) params := &api.CreateMessageParams{} params.SetContentSid("HT**********************70") params.SetMessagingServiceSid("MG******************************0d") params.SetFrom("whatsapp:+917*******2") params.SetTo("whatsapp:+917********4") resp, err := client.Api.CreateMessage(params) if err != nil { fmt.Println(err.Error()) } else { if resp.Sid != nil { fmt.Println(*resp.Sid) } else { fmt.Println(resp.Sid) } } }
The error I receive is -
Status: 400 - ApiError 20422: Invalid Parameter (null) More info: https://www.twilio.com/docs/errors/20422
I get the same error if I try to do it via Postman.
Error20422 means that one of the three conditions is not met:
Since you are using the SDK, it is most likely the third bullet point. Is there a reason why you are using the MessagingServiceSid
and From
fields? I recommend updating the client to the latest version and running this script:
package main import ( "fmt" "log" "os" "github.com/joho/godotenv" "github.com/twilio/twilio-go" api "github.com/twilio/twilio-go/rest/api/v2010" ) func main() { err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } client := twilio.NewRestClient() params := &api.CreateMessageParams{} params.SetTo("whatsapp:"+os.Getenv("RECIPIENT_PHONE_NUMBER")) params.SetFrom(os.Getenv("TWILIO_MESSAGING_SERVICE")) params.SetContentSid(os.Getenv("CONTENT_SID")) _, err = client.Api.CreateMessage(params) if err != nil { fmt.Println(err.Error()) } else { fmt.Println("Message sent successfully!") } }
The above is the detailed content of Trying Twilio Whatsappp api call but getting error 20422. For more information, please follow other related articles on the PHP Chinese website!