SendGrid's Golang HTTP Client for calling APIs

Related tags

Web Frameworks rest
Overview

SendGrid Logo

Build Status GoDoc Go Report Card Email Notifications Badge Twitter Follow GitHub contributors MIT licensed

Quickly and easily access any RESTful or RESTful-like API.

If you are looking for the SendGrid API client library, please see this repo.

Announcements

The default branch name for this repository has been changed to main as of 07/27/2020.

All updates to this library is documented in our CHANGELOG.

Table of Contents

Installation

Prerequisites

  • Go version 1.6.X, 1.7.X, 1.8.X, 1.9.X or 1.10.X

Install Package

go get github.com/sendgrid/rest

Setup Environment Variables

Initial Setup

cp .env_sample .env

Environment Variable

Update the development environment with your SENDGRID_API_KEY, for example:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

With Docker

A Docker image has been created to allow you to get started with rest right away.

docker-compose up -d --build

# Ensure the container is running with 'docker ps'
docker ps
CONTAINER ID        IMAGE               COMMAND               CREATED              STATUS              PORTS               NAMES
40c8d984a620        rest_go             "tail -f /dev/null"   About a minute ago   Up About a minute                       rest_go_1

With the container running, you can execute your local go scripts using the following:

# docker exec <container_name> <go command>
docker exec rest_go_1 go run docker/example.go
200
{
  "args": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "origin": "86.180.177.202",
  "url": "https://httpbin.org/get"
}

map[Access-Control-Allow-Origin:[*] Access-Control-Allow-Credentials:[true] Via:[1.1 vegur] Connection:[keep-alive] Server:[gunicorn/19.9.0] Date:[Tue, 02 Oct 2018 18:20:43 GMT] Content-Type:[application/json] Content-Length:[233]]

# You can install libraries too, using the same command
# NOTE: Any libraries installed will be removed when the container is stopped.
docker exec rest_go_1 go get github.com/uniplaces/carbon

Your go files will be executed relative to the root of this directory. So in the example above, to execute the example.go file within the docker directory, we run docker exec rest_go_1 go run docker/example.go. If this file was in the root of this repository (next to README.exe, rest.go etc.), you would run docker exec rest_go_1 go run my_go_script.go

Quick Start

GET /your/api/{param}/call

package main

import "github.com/sendgrid/rest"
import "fmt"

func main() {
	const host = "https://api.example.com"
	param := "myparam"
	endpoint := "/your/api/" + param + "/call"
	baseURL := host + endpoint
	method := rest.Get
	request := rest.Request{
		Method:  method,
		BaseURL: baseURL,
	}
	response, err := rest.Send(request)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}

POST /your/api/{param}/call with headers, query parameters and a request body.

package main

import "github.com/sendgrid/rest"
import "fmt"

func main() {
	const host = "https://api.example.com"
	param := "myparam"
	endpoint := "/your/api/" + param + "/call"
	baseURL := host + endpoint
	Headers := make(map[string]string)
	key := os.Getenv("API_KEY")
	Headers["Authorization"] = "Bearer " + key
	Headers["X-Test"] = "Test"
	var Body = []byte(`{"some": 0, "awesome": 1, "data": 3}`)
	queryParams := make(map[string]string)
	queryParams["hello"] = "0"
	queryParams["world"] = "1"
	method := rest.Post
	request = rest.Request{
		Method:      method,
		BaseURL:     baseURL,
		Headers:     Headers,
		QueryParams: queryParams,
		Body:        Body,
	}
	response, err := rest.Send(request)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}

Usage

How to Contribute

We encourage contribution to our projects, please see our CONTRIBUTING guide for details.

Quick links:

About

rest is maintained and funded by Twilio SendGrid, Inc. The names and logos for rest are trademarks of Twilio SendGrid, Inc.

If you need help installing or using the library, please check the Twilio SendGrid Support Help Center.

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

License

The MIT License (MIT)

Comments
  • Unit test to check some files

    Unit test to check some files

    Fixes #52

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [x] I have added necessary documentation about the functionality in the appropriate .md file
    • [x] I have added in line documentation to the code I modified

    Short description of what this PR does:

    Create test to check some repo files

    status: code review request difficulty: medium 
    opened by gabrielclima 8
  • feat: do not swallow the error code

    feat: do not swallow the error code

    When io.ReadAll fails we get no information on the request.

    It's annoying, as error code are usually really useful in those case.

    Checklist

    • [X] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [X] I have read the [Contribution Guide] and my PR follows them.
    • [X] I updated my branch with the master branch.
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [ ] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • Returns the response object with an error to help treat the error
    type: community enhancement status: code review request difficulty: medium 
    opened by Succo 6
  • docs: Run Grammarly on *.md files

    docs: Run Grammarly on *.md files

    Fixes #76

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [ ] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • Runs Grammarly on *.md files.
    • Left things that Grammarly wasn't too sure about as they are.
    • Improved Grammar myself in one instance.

    If you have questions, please send an email to Sendgrid, or file a Github Issue in this repository.

    status: code review request difficulty: medium type: docs update 
    opened by obahareth 5
  • Add fallback functionality for Client.API()

    Add fallback functionality for Client.API()

    Fixes

    Fixes #67

    Checklist

    • [ ] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [x] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • While the recent change from #19 added fallback functionality for the global available API function, it did not add fallback functionality for the API function in the Client, breaking the Sendgrid Go client and possibly other implementations.
    status: code review request difficulty: medium 
    opened by jerbob92 5
  • docs: added

    docs: added "Code Review" section to CONTRIBUTING.md

    Fixes #64

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [ ] I have added in line documentation to the code I modified

    Short description of what this PR does:

    Added "Code Reviews" section as #64 suggests.

    If you have questions, please send an email to Sendgrid, or file a Github Issue in this repository.

    difficulty: easy type: docs update status: ready for deploy 
    opened by aleien 5
  • Fixes #60 - Update .travis.yml for Codecov

    Fixes #60 - Update .travis.yml for Codecov

    Fixes #60

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.

    Short description of what this PR does:

    • Adds CodeCov support into .travis.yml.
    status: code review request difficulty: easy 
    opened by mrmuskrat 5
  • Add USAGE.md

    Add USAGE.md

    Hey, I added an USAGE.md file, as described in #46 . Also, I have modified README.md file to mention the introduced file with usage examples.

    Hope, I will be of any help for you, guys :smile:

    status: code review request difficulty: easy 
    opened by sadliak 5
  • docs: update contribution guide with new workflow

    docs: update contribution guide with new workflow

    Fixes

    Closes #81

    Checklist

    • [ ] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [ ] I have read the [Contribution Guide] and my PR follows them.
    • [ ] I updated my branch with the master branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [ ] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • Updated PR template to match new git worklfow
    • Updated CONTRIBUTING.md to match new git workflow

    If you have questions, please send an email to Sendgrid, or file a Github Issue in this repository.

    difficulty: easy type: docs update status: ready for deploy 
    opened by radlinskii 4
  • chore: create README.md in use-cases

    chore: create README.md in use-cases

    Fixes

    Fixes #66

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [x] I have added necessary documentation about the functionality in the appropriate .md file
    • [ ] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • Creates the README.md in the use-cases directory that I created .
    • Starts off the fix #66
    difficulty: easy type: twilio enhancement status: ready for deploy 
    opened by ajloria 4
  • typo fix in CHANGELOG.md

    typo fix in CHANGELOG.md

    Fixes

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the master branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [ ] I have added in line documentation to the code I modified

    Short description of what this PR does:

    • "explicitely" is a misspelling of "explicitly"
    difficulty: easy type: docs update 
    opened by nasa9084 4
  • Script to generate changelog entries for new releases

    Script to generate changelog entries for new releases

    Fixes #79

    Checklist

    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the [Contribution Guide] and my PR follows them.
    • [x] I updated my branch with the development branch.
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added necessary documentation about the functionality in the appropriate .md file
    • [x] I have added in line documentation to the code I modified

    Short description of what this PR does:

    Added a script to generate a changelog entry for a new release with the list of merged PRs since the latest tagged release included.

    As the original issue did not have detailed requirements, I made the following decisions during the implementation:

    • The "script" is written in Go and git is its only dependency, as these are the 2 things which are expected to be available in any machine used for working on this repository
    • As previous changelog entries mainly included single PRs I came up with a suggested format for handling multiple PRs per release
    • The generated entry does not include the full name of PR authors for now, because this either needs communication with the GitHub API or some extra parsing locally through git
    status: code review request 
    opened by theag3nt 3
  • feat: Add Go Modules and add Go 1.19 compatibility

    feat: Add Go Modules and add Go 1.19 compatibility

    Fixes

    Adds Go Modules, and also adding a v2 directory as per current golang standards as per https://go.dev/blog/v2-go-modules.

    This PR also makes minor adjustments to correct compatibility with Go 1.19.

    Checklist

    • [x] I acknowledge that all my contributions will be made under the project's license
    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the Contribution Guidelines and my PR follows them
    • [x] I have titled the PR appropriately
    • [x] I have updated my branch with the main branch
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [x] I have added the necessary documentation about the functionality in the appropriate .md file
    • [x] I have added inline documentation to the code I modified

    If you have questions, please file a support ticket.

    opened by AlaricWhitney 0
  • Feature: request context

    Feature: request context

    Closes #403

    Feature

    Adds a SendWithContext function and a context field to the request object as requested.

    Checklist

    • [x] I acknowledge that all my contributions will be made under the project's license
    • [x] I have made a material change to the repo (functionality, testing, spelling, grammar)
    • [x] I have read the Contribution Guidelines and my PR follows them
    • [x] I have titled the PR appropriately
    • [x] I have updated my branch with the main branch
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [ ] I have added the necessary documentation about the functionality in the appropriate .md file
    • [x] I have added inline documentation to the code I modified

    If you have questions, please file a support ticket, or create a GitHub Issue in this repository.

    status: code review request 
    opened by krijohs 0
Releases(v2.6.9)
Owner
Twilio SendGrid
Official open source libraries - send emails and integrate with our APIs fast.
Twilio SendGrid
⚓️ Golang RESTful APIs

Golang RESTful Golang RESTful API started on January 23rd, 2022. 环境配置相关解决办法

Hou Wenbo 0 Jan 23, 2022
GoPrisma - A Go wrapper for prisma to turn databases into GraphQL APIs using Go.

GoPrisma - a Go wrapper for the Prisma Engines What's this? Introspect a database and use it as a GraphQL API using Go. Supported Databases: SQLite Po

Jens Neuse 71 Dec 20, 2022
Generate boilerplate + endpoints for Fiber REST APIs.

gomakeme Generate boilerplate + endpoints for Fiber REST APIs. Never spend 6 minutes doing something by hand when you can spend 1 week to automate it

null 5 Dec 30, 2022
⚡ Rux is an simple and fast web framework. support middleware, compatible http.Handler interface. 简单且快速的 Go web 框架,支持中间件,兼容 http.Handler 接口

Rux Simple and fast web framework for build golang HTTP applications. NOTICE: v1.3.x is not fully compatible with v1.2.x version Fast route match, sup

Gookit 84 Dec 8, 2022
A minimal framework to build web apps; with handler chaining, middleware support; and most of all standard library compliant HTTP handlers(i.e. http.HandlerFunc).

WebGo v4.1.3 WebGo is a minimalistic framework for Go to build web applications (server side) with zero 3rd party dependencies. Unlike full-fledged fr

Kamaleshwar 266 Jan 1, 2023
Muxie is a modern, fast and light HTTP multiplexer for Go. Fully compatible with the http.Handler interface. Written for everyone.

Muxie ?? ?? ?? ?? ?? ?? Fast trie implementation designed from scratch specifically for HTTP A small and light router for creating sturdy backend Go a

Gerasimos (Makis) Maropoulos 280 Dec 8, 2022
Gin is a HTTP web framework written in Go (Golang).

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Gin-Gonic 65.5k Jan 3, 2023
Tigo is an HTTP web framework written in Go (Golang).It features a Tornado-like API with better performance. Tigo是一款用Go语言开发的web应用框架,API特性类似于Tornado并且拥有比Tornado更好的性能。

Tigo(For English Documentation Click Here) 一个使用Go语言开发的web框架。 相关工具及插件 tiger tiger是一个专门为Tigo框架量身定做的脚手架工具,可以使用tiger新建Tigo项目或者执行其他操作。

Karl 1.4k Jan 5, 2023
Opinionated boilerplate Golang HTTP server with CORS, OPA, Prometheus, rate-limiter for API and static website.

Teal.Finance/Garcon Opinionated boilerplate HTTP server with CORS, OPA, Prometheus, rate-limiter… for API and static website. Origin This library was

Teal.Finance 8 Nov 3, 2022
HTTP Rest API in Golang

Database configuration (for MacOS) Useful article - click to read Install postgres Run cd /Library/PostgreSQL/13/bin Run sudo -u postgres ./createdb r

null 0 Dec 14, 2021
Lightweight web framework based on net/http.

Goweb Light weight web framework based on net/http. Includes routing middleware logging easy CORS (experimental) Goweb aims to rely only on the standa

Travis Harmon 34 Dec 21, 2022
Gerasimos (Makis) Maropoulos 23.4k Dec 28, 2022
lightweight, idiomatic and composable router for building Go HTTP services

chi is a lightweight, idiomatic and composable router for building Go HTTP services. It's especially good at helping you write large REST API services

go-chi 13k Jan 6, 2023
Headless CMS with automatic JSON API. Featuring auto-HTTPS from Let's Encrypt, HTTP/2 Server Push, and flexible server framework written in Go.

Ponzu Watch the video introduction Ponzu is a powerful and efficient open-source HTTP server framework and CMS. It provides automatic, free, and secur

Ponzu 5.5k Dec 28, 2022
Paramex is a library that binds http request parameters to a Go struct annotated with `param`.

paramex Paramex is a library that binds http request parameters to a Go struct annotated with param. Description To extract http parameters (headers,

Nipuna Pathirana 7 Oct 31, 2022
Rocinante is a gin inspired web framework built on top of net/http.

Rocinante Rocinante is a gin inspired web framework built on top of net/http. ⚙️ Installation $ go get -u github.com/fskanokano/rocinante-go ⚡️ Quicks

null 3 Jul 27, 2021
Timeout handler for http request in Gin framework

Middleware to Handle Request Timeout in Gin Installation Installation go get github.com/s-wijaya/gin-timeout Import it in your code: import ( // o

Saptari Wijaya 0 Dec 14, 2021
Isomorphic Go client for PostgREST.

Postgrest GO Golang client for PostgREST. The goal of this library is to make an "ORM-like" restful interface. Documentation Full documentation can be

Supabase 97 Dec 31, 2022
RPC Framework abstraction layer. Provides foundation of the RonyDesc to generate RPC server/client codes.

RonyKit RonyKit provides the abstraction layer for creating a cluster aware API server. By defining separate components for each task, you are almost

Ronak Software Group 5 Dec 15, 2022