Go quick start
Last updated: Jan-03-2023
This quick start is intended to let you quickly try using several common Cloudinary features. It doesn't necessarily employ coding best practices and the code you create here isn't intended for production.
You can perform this quick start in a code sandbox or in a clean project in the dev environment of your choice.
You can also view the completed code of this quick start in a GitHub repo and code explorer.
You may also find our Glossary helpful to understand Cloudinary-specific terminology.
Prerequisites
To perform the steps in this quick start, you'll need:
- A Cloudinary account. If you don't have one yet, you can quickly register for free.
- Your product environment credentials. You can find your credentials in the Dashboard page of your Cloudinary Console.
- A working Go development environment with a supported version of Go.
1. Set up and configure the SDK
Create a go.mod file in the directory where your Go program will be saved:
In a terminal in your Go environment, run the following code:
In a terminal, set your CLOUDINARY_URL
environment variable.
Replace CLOUDINARY_URL=cloudinary://API_KEY:API_SECRET@CLOUD_NAME
with the API environment variable copied from your product environment credentials:
On Mac or Linux:
On Windows:
- When writing your own applications, follow your organization's policy on storing secrets and don't expose your API secret.
- If you use a method that involves writing your environment variable to a file (e.g.
dotenv
), the file should be excluded from your version control system, so as not to expose it publicly.
In your project, create a new file called my_file.go
. Copy and paste the following into this file:
package main // Import Cloudinary and other necessary libraries //=================== import ( "context" "fmt" "github.com/cloudinary/cloudinary-go/v2" "github.com/cloudinary/cloudinary-go/v2/api" "github.com/cloudinary/cloudinary-go/v2/api/admin" "github.com/cloudinary/cloudinary-go/v2/api/uploader" ) func credentials() (*cloudinary.Cloudinary, context.Context) { // Add your Cloudinary credentials, set configuration parameter // Secure=true to return "https" URLs, and create a context //=================== cld, _ := cloudinary.New() cld.Config.URL.Secure = true ctx := context.Background() return cld, ctx }
2. Upload an image
Copy and paste this into my_file.go
:
func uploadImage(cld *cloudinary.Cloudinary, ctx context.Context) { // Upload the image. // Set the asset's public ID and allow overwriting the asset with new versions resp, err := cld.Upload.Upload(ctx, "https://cloudinary-devs.github.io/cld-docs-assets/assets/images/butterfly.jpeg", uploader.UploadParams{ PublicID: "quickstart_butterfly", UniqueFilename: api.Bool(false), Overwrite: api.Bool(true)}) if err != nil { fmt.Println("error") } // Log the delivery URL fmt.Println("****2. Upload an image****\nDelivery URL:", resp.SecureURL, "\n") }
3. Get and use details of the image
Copy and paste this into my_file.go
:
func getAssetInfo(cld *cloudinary.Cloudinary, ctx context.Context) { // Get and use details of the image // ============================== resp, err := cld.Admin.Asset(ctx, admin.AssetParams{PublicID: "quickstart_butterfly"}) if err != nil { fmt.Println("error") } fmt.Println("****3. Get and use details of the image****\nDetailed response:\n", resp, "\n") // Assign tags to the uploaded image based on its width. Save the response to the update in the variable 'update_resp'. if resp.Width > 900 { update_resp, err := cld.Admin.UpdateAsset(ctx, admin.UpdateAssetParams{ PublicID: "quickstart_butterfly", Tags: []string{"large"}}) if err != nil { fmt.Println("error") } else { // Log the new tag to the console. fmt.Println("New tag: ", update_resp.Tags, "\n") } } else { update_resp, err := cld.Admin.UpdateAsset(ctx, admin.UpdateAssetParams{ PublicID: "quickstart_butterfly", Tags: []string{"small"}}) if err != nil { fmt.Println("error") } else { // Log the new tag to the console. fmt.Println("New tag: ", update_resp.Tags, "\n") } } }
4. Transform the image
Copy and paste this into my_file.go
:
func transformImage(cld *cloudinary.Cloudinary, ctx context.Context) { // Instantiate an object for the asset with public ID "my_image" qs_img, err := cld.Image("quickstart_butterfly") if err != nil { fmt.Println("error") } // Add the transformation qs_img.Transformation = "r_max/e_sepia" // Generate and log the delivery URL new_url, err := qs_img.String() if err != nil { fmt.Println("error") } else { print("****4. Transform the image****\nTransfrmation URL: ", new_url, "\n") } }
5. Run your code
Copy and paste this into my_file.go
:
func main() {
cld, ctx := credentials()
uploadImage(cld, ctx)
getAssetInfo(cld, ctx)
transformImage(cld, ctx)
}
In the terminal, run the following command:
The following original image is uploaded to your Cloudinary account, tagged appropriately and accessible via the URL shown below.
The transformed version of the image is accessible via the URL shown below.

http://res.cloudinary.com/<cloud-name>/image/
upload/v1/quickstart_butterfly

http://res.cloudinary.com/<cloud-name>
/image/upload/e_sepia
r_max/v1/quickstart_butterfly
View the completed code
See the code above in action using this code playground.
- Click Remix to Edit.
- Copy your API Environment variable value (i.e., only the portion after the equal sign,
cloudinary://<api_key:api_secret@cloud_name
). - Paste it into the Glitch
.env
file as theVariable Value
forCLOUDINARY_URL
. - Click Logs at the bottom of the screen.
This code is also available in GitHub
Next steps
- Learn more about the Go SDK by visiting the other pages in this SDK guide.
- Get comprehensive details about Cloudinary features and capabilities:
- Media upload guide: Provides details and examples of the upload options.
- Image transformations guide: Provides details and examples of the transformations you can apply to image assets.
- Video transformations guide: Provides details and examples of the transformations you can apply to video assets.
- Transformation URL API Reference: Provides details and examples of all available transformation parameters.
- Admin API guide: Provides details and examples of the methods available for managing and organizing your media assets.