Facebook Marketing API SDK for Golang
This go package provides a comprehensive list of methods for interacting with Facebook's Graph Marketing api.
You enjoy working with the Facebook Marketing API? We are hiring!
Find out what open positions we have at the moment and why JustWatch is a great place to work at: https://www.justwatch.com/us/talent
The SDK supports crud operations for the following entities:
- ad_account
- adset
- custom_conversion
- insights
- post
- videos
- adcreative
- audience
- event
- interest
- search
- ads
- campaign
- image
- page
- service
Usage
We assume you already have a Facebook Developer account and configured an accessToken
and appSecret
. If not, checkout Facebooks Get Started
Create a new fbService client
import(
"github.com/pedrolpin4/facebook-marketing-api-golang-sdk/marketing/v16"
)
func main(){
fbService, _ := v16.New(l, accessToken, appSecret)
}
Create a campaign
c := v16.Campaign{
}
id, _ := fbService.Campaigns.Create(ctx, c)
Upload an external asset to Facebook
accountID := "[account id]"
imagePath := "[path to image]"
imageName := "[image name]"
file, _ := os.Open(imagePath)
im, _ := fbService.Images.Upload(context.Background(), accountID,imageName, file)
fmt.Println("Uploaded image has id: ", im.ID)
accountID := "[account id]"
videoPath := "[path to video]"
videoName := "[video name]"
file, _ := os.Open(videoPath)
vid, _ := fbService.Videos.Upload(context.Background(), accountID, videoName, file)
fmt.Println("Uploaded video has id: ", vid.ID)
Read campaigns from an account
id := "[account_id]"
campaigns, _ := p.fbService.Campaigns.List(id).Do(ctx)
Get reporting data for an account at adset level
columns := []string {}
id := "[account_id]"
report := fbService.Insights.NewReport(id)
report.Level("adset").
DailyTimeIncrement(true).
Fields(columns...).
DatePreset("lifetime")
ch := make(chan v16.Insight)
nRecords,_ := report.GenerateReport(ctx,ch)
for insight := range ch {
fmt.Println("New report result: ", insight)
}