> For the complete documentation index, see [llms.txt](https://docs.go-atlassian.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.go-atlassian.io/cookbooks/get-user-last-login-info.md).

# Get User Last-Login Info

In this article, I would be showing you how to extract user last-activity information from the Atlassian Admin API's using `go-atlassian`

## Step 1: Create a new Go project

Create a new directory for your project and navigate to it in your terminal or command prompt. Initialize a new Go module using the following command:

```bash
go mod init your-module-name
```

## Step 2: Install the "go-atlassian" library&#x20;

To use the "go-atlassian" library, you need to install it as a dependency in your project. Run the following command:

```bash
go get github.com/ctreminiom/go-atlassian
```

## Step 3: Import the required packages&#x20;

Create a new Go file, e.g., `main.go`, and import the necessary packages:

{% code fullWidth="true" %}

```go
package main

import (
	"fmt"
	"log"

	"github.com/ctreminiom/go-atlassian/admin"
)
```

{% endcode %}

## Step 4: Set up Cloud Admin client&#x20;

Initialize the Atlassian Admin client with your API token:

{% code fullWidth="true" %}

```go
func main() {
	//https://support.atlassian.com/organization-administration/docs/manage-an-organization-with-the-admin-apis/
	var apiKey = os.Getenv("ATLASSIAN_ADMIN_TOKEN")

	cloudAdmin, err := admin.New(nil)
	if err != nil {
		log.Fatal(err)
	}

	cloudAdmin.Auth.SetBearerToken(apiKey)
	cloudAdmin.Auth.SetUserAgent("curl/7.54.0")
}
```

{% endcode %}

## Step 5: Extract the organization ID

It's required to extract the ID of the organization you're using, the organization ID can be extract on the admin URL, as the image below:

<figure><img src="https://4039951191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MRIBKheqm91_IPQsFV_%2Fuploads%2F7SjL9MKO9SfyglrNbV91%2Fimage.png?alt=media&amp;token=c125165f-56ba-43ef-9793-8125899ba229" alt=""><figcaption></figcaption></figure>

## Step 6: Extract the organization users

In this step, we're going to extract the organization users and store them into an array variable, please use the following code block to extract them.

{% code fullWidth="true" %}

```go
// Extract the organization users
var (
	organizationID = "9a1jj823-jac8-123d-jj01-63315k059cb2"
	cursor         string
	users          []*models.AdminOrganizationUserScheme
)

for {

	page, response, err := cloudAdmin.Organization.Users(context.Background(), organizationID, cursor)
	if err != nil {
		if response != nil {
			log.Println("Response HTTP Response", response.Bytes.String())
		}
		log.Fatal(err)
	}

	users = append(users, page.Data...)

	if len(page.Links.Next) == 0 {
		break
	}

	//extract the next cursor pagination
	nextAsURL, err := url.Parse(page.Links.Next)
	if err != nil {
		log.Fatal(err)
	}

	cursor = nextAsURL.Query().Get("cursor")
}

for _, user := range users {
	log.Println("Name:", user.Name, " -- ID:", user.AccountID)
}
```

{% endcode %}

## Step 7: Extract the last-login date

With the organization users extracted, we need to iterate each user and extract the last-login information, you can use the following code to do that:

{% code fullWidth="true" %}

```go
for _, user := range users {

	lastLogin, response, err := cloudAdmin.Organization.Directory.Activity(context.Background(), organizationID, user.AccountID)
	if err != nil {
		if response != nil {
			log.Println("Response HTTP Response", response.Bytes.String())
		}
		log.Fatal(err)
	}

	// The products are iterated because an Atlassian user may contain multiple products
	// If you're looking to a specific product, you can modify the loop logic and extract
	// the last-login for Jira or Confluence
	for _, product := range lastLogin.Data.ProductAccess {

		fmt.Println(lastLogin.Data.AddedToOrg, product)
	}
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.go-atlassian.io/cookbooks/get-user-last-login-info.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
