Skip to content
This documentation applies to Codacy Self-hosted v12.0.0

For the latest updates and improvements, see the latest Cloud documentation instead.

Creating project API tokens programmatically#

To create new project API tokens for your Codacy repositories programmatically, use the Codacy API endpoint createRepositoryApiToken.

For example, if you're setting up coverage for all your repositories and prefer not to use a single account API token that grants the same permissions as an administrator, you need to create an individual project API token for each repository.

Example: Creating project API tokens for all repositories in an organization#

This example creates new project API tokens for all the repositories in an organization and outputs a comma-separated list of repository names and corresponding token strings.

The example script:

  1. Defines the account API token used to authenticate on the Codacy API, the Git provider, and the organization name.
  2. Calls the Codacy API endpoint listOrganizationRepositories to retrieve the list of repositories in the organization.
  3. Uses jq to select only the name of the repositories.
  4. Asks for confirmation from the user before making any changes.
  5. For each repository, calls the Codacy API endpoint createRepositoryApiToken to create a new project API token and uses jq to obtain only the created token string.
  6. Outputs a comma-separated list of the repository names and the corresponding new token strings.
  7. Pauses a few seconds between requests to the Codacy API to avoid rate limiting.
#!/bin/bash

CODACY_API_TOKEN="<your account API token>"
GIT_PROVIDER="<your Git provider>" # gh, ghe, gl, gle, bb, or bbe
ORGANIZATION="<your organization name>"

repositories=$(curl -sX GET "https://app.codacy.com/api/v3/organizations/$GIT_PROVIDER/$ORGANIZATION/repositories" \
                    -H "api-token: $CODACY_API_TOKEN" \
               | jq -r ".data[] | .name")

count=$(echo "$repositories" | wc -l)
read -p "Create project tokens for $count repositories? (y/n) " choice
if [ "$choice" = "y" ]; then
    echo "$repositories" | while read repository; do
        echo -n "$repository,"
        curl -sX POST "https://app.codacy.com/api/v3/organizations/$GIT_PROVIDER/$ORGANIZATION/repositories/$repository/tokens" \
             -H "api-token: $CODACY_API_TOKEN" \
        | jq -r ".data | .token"
        sleep 2 # Wait 2 seconds
    done
else
    echo "No changes made.";
fi

Example output:

chart,<new project API token>
docs,<new project API token>
website,<new project API token>
[...]

Important

For the sake of simplicity, the example doesn't consider paginated results obtained from the Codacy API. Learn how to use pagination to ensure that you process all results returned by the API.

See also#

Share your feedback 📢

Did this page help you?

Thanks for the feedback! Is there anything else you'd like to tell us about this page?

We're sorry to hear that. Please let us know what we can improve:

Alternatively, you can create a more detailed issue on our GitHub repository.

Thanks for helping improve the Codacy documentation.

If you have a question or need help please contact support@codacy.com.

Last modified July 13, 2022