Introducing devtogo: A wrapper for the dev.to api written in Go
Tags: go, api, devto
Published: 2019-10-28 09:57:28

> This post will introduce you to devtogo and will show you some usage examples.

I've been using Go as my main programming language for about six months at work. It's been very enjoyable and I always learn something new every day.

I **wrote devtogo as a means for me to learn** how to write some good opensource Go code. It's also **my very first opensource project** that I am publishing. Much of my work is sitting in private repositories, so I thought that I may as well expose this for the world to use.

Try it out and contribute/interact with the Github repo.

{% github https://github.com/ShiraazMoollatjie/devtogo %}

## Overview

**Devtogo is an rest api wrapper** for the dev.to api. It can be used to publish articles or even search for articles of a specific tag. There are many usecases for using the dev.to api.

## Creating a client

A **client can be created anonymously or via credentials**. The only credentials supported at the moment are `api keys` and one can can obtain them [by having a look at the authentication section of the api.](https://docs.dev.to/api/#section/Authentication)

To create a client, we simply use the `NewClient` function and specify the api key.
```go
cl := devtogo.NewClient(devtogo.WithApiKey("MY_API_KEY"))
```

## Retrieving articles

It's possible to **retrieve either a list of articles or a single article**. For this post, we are going to retrieve all the articles that are tagged with `go`

```go
al, err := cl.GetArticles(devtogo.Arguments{
		"tag": "go",
	})
```

### Retrieving articles owned by you

If we are an authenticated user, we can **retrieve our own list** of published or unpublished articles. This is **useful** for obtaining a list of our drafts that we want to publish later.

```go
al, err := cl.GetMyUnpublishedArticles(devtogo.Defaults())
	if err != nil {
		panic(err)
	}
```

## Creating and updating articles
To create an article, we use the `CreateArticle` function. This will **create a new article on the dev.to platform**. 

```go
np, err := cl.CreateArticle(devtogo.CreateArticle{
  Title:        "My new dev.to post",
  Tags:         []string{"go"},
  BodyMarkdown: "my long markdown article that is preferably read from a file",
})
```

In the above example, we create an article that is in a `draft` state with a `title`, some `tags` and some markdown.

To publish this article on create, we set the `Published` property to true.

Supposed we want to **update this article** by publishing it, we simply use the `UpdateArticle` function. 

```go
ua, err := cl.UpdateArticle(np.ID, devtogo.CreateArticle{
		Title:        "My updates dev.to post using the API",
		BodyMarkdown: "my new updated content",
		Published:    true,
	})
```

## Conclusion

In this article, we learnt how to use the devtogo to interact with the dev.to platform using the Go programming language. We also about how to use devtogo.

## Full example
This is a **full working example** that retrieves the latest Go articles.

```go
package main

import (
  "fmt"
  "github.com/ShiraazMoollatjie/devtogo"
  "log"
  "os"
)

func main() {
  apiToken := os.Getenv("DEVTO_TOKEN")
  if apiToken == "" {
    log.Fatalf("Set the DEVTO_TOKEN")
  }
  dtgc := devtogo.NewClient(devtogo.WithApiKey(apiToken))

  al, err := dtgc.GetArticles(devtogo.Arguments{
    "tag": "go",
  })
  if err != nil {
    log.Fatalf("Cannot retrieve articles: %+v", err)
  }

  for _,a :=range al {
    fmt.Printf("Title: %s, Url: %s\n", a.Title, a.URL)
  }
}
```

## References
[Dev.to API Documentation](https://docs.dev.to/api)