# Snippets ## List zone snippets `client.Snippets.List(ctx, params) (*V4PagePaginationArray[SnippetListResponse], error)` **get** `/zones/{zone_id}/snippets` Fetches all snippets belonging to the zone. ### Parameters - `params SnippetListParams` - `ZoneID param.Field[string]` Path param: Use this field to specify the unique ID of the zone. - `Page param.Field[int64]` Query param: Specifies the current page number. - `PerPage param.Field[int64]` Query param: Specifies how many results to return per page. ### Returns - `type SnippetListResponse struct{…}` Define a snippet. - `CreatedOn Time` Indicates when the snippet was created. - `SnippetName string` Identify the snippet. - `ModifiedOn Time` Indicates when the snippet was last modified. ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" "github.com/cloudflare/cloudflare-go/snippets" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) page, err := client.Snippets.List(context.TODO(), snippets.SnippetListParams{ ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"), }) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", page) } ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": [ { "created_on": "2000-01-01T00:00:00.000000Z", "snippet_name": "my_snippet", "modified_on": "2000-01-01T00:00:00.000000Z" } ], "success": true, "result_info": { "count": 25, "page": 1, "per_page": 25, "total_count": 100, "total_pages": 10 } } ``` ## Get a zone snippet `client.Snippets.Get(ctx, snippetName, query) (*SnippetGetResponse, error)` **get** `/zones/{zone_id}/snippets/{snippet_name}` Fetches a snippet belonging to the zone. ### Parameters - `snippetName string` Identify the snippet. - `query SnippetGetParams` - `ZoneID param.Field[string]` Use this field to specify the unique ID of the zone. ### Returns - `type SnippetGetResponse struct{…}` Contain the response result. - `CreatedOn Time` Indicates when the snippet was created. - `SnippetName string` Identify the snippet. - `ModifiedOn Time` Indicates when the snippet was last modified. ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" "github.com/cloudflare/cloudflare-go/snippets" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) snippet, err := client.Snippets.Get( context.TODO(), "my_snippet", snippets.SnippetGetParams{ ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"), }, ) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", snippet.CreatedOn) } ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": { "created_on": "2000-01-01T00:00:00.000000Z", "snippet_name": "my_snippet", "modified_on": "2000-01-01T00:00:00.000000Z" }, "success": true } ``` ## Update a zone snippet `client.Snippets.Update(ctx, snippetName, params) (*SnippetUpdateResponse, error)` **put** `/zones/{zone_id}/snippets/{snippet_name}` Creates or updates a snippet belonging to the zone. ### Parameters - `snippetName string` Identify the snippet. - `params SnippetUpdateParams` - `ZoneID param.Field[string]` Path param: Use this field to specify the unique ID of the zone. - `Metadata param.Field[SnippetUpdateParamsMetadata]` Body param: Provide metadata about the snippet. - `MainModule string` Specify the name of the file that contains the main module of the snippet. ### Returns - `type SnippetUpdateResponse struct{…}` Contain the response result. - `CreatedOn Time` Indicates when the snippet was created. - `SnippetName string` Identify the snippet. - `ModifiedOn Time` Indicates when the snippet was last modified. ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" "github.com/cloudflare/cloudflare-go/snippets" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) snippet, err := client.Snippets.Update( context.TODO(), "my_snippet", snippets.SnippetUpdateParams{ ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"), Metadata: cloudflare.F(snippets.SnippetUpdateParamsMetadata{ MainModule: cloudflare.F("main.js"), }), }, ) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", snippet.CreatedOn) } ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": { "created_on": "2000-01-01T00:00:00.000000Z", "snippet_name": "my_snippet", "modified_on": "2000-01-01T00:00:00.000000Z" }, "success": true } ``` ## Delete a zone snippet `client.Snippets.Delete(ctx, snippetName, body) (*SnippetDeleteResponse, error)` **delete** `/zones/{zone_id}/snippets/{snippet_name}` Deletes a snippet belonging to the zone. ### Parameters - `snippetName string` Identify the snippet. - `body SnippetDeleteParams` - `ZoneID param.Field[string]` Use this field to specify the unique ID of the zone. ### Returns - `type SnippetDeleteResponse interface{…}` Contain the response result. ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" "github.com/cloudflare/cloudflare-go/snippets" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) snippet, err := client.Snippets.Delete( context.TODO(), "my_snippet", snippets.SnippetDeleteParams{ ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"), }, ) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", snippet) } ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` # Content ## Get a zone snippet content `client.Snippets.Content.Get(ctx, snippetName, query) (*Response, error)` **get** `/zones/{zone_id}/snippets/{snippet_name}/content` Fetches the content of a snippet belonging to the zone. ### Parameters - `snippetName string` Identify the snippet. - `query ContentGetParams` - `ZoneID param.Field[string]` Use this field to specify the unique ID of the zone. ### Returns - `type ContentGetResponse interface{…}` ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" "github.com/cloudflare/cloudflare-go/snippets" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) content, err := client.Snippets.Content.Get( context.TODO(), "my_snippet", snippets.ContentGetParams{ ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"), }, ) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", content) } ``` # Rules ## List zone snippet rules `client.Snippets.Rules.List(ctx, query) (*RuleListResponse, error)` **get** `/zones/{zone_id}/snippets/snippet_rules` Fetches all snippet rules belonging to the zone. ### Parameters - `query RuleListParams` - `ZoneID param.Field[string]` Use this field to specify the unique ID of the zone. ### Returns - `type RuleListResponse interface{…}` Contain the response result. ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" "github.com/cloudflare/cloudflare-go/snippets" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) rules, err := client.Snippets.Rules.List(context.TODO(), snippets.RuleListParams{ ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"), }) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", rules) } ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` ## Update zone snippet rules `client.Snippets.Rules.Update(ctx, params) (*RuleUpdateResponse, error)` **put** `/zones/{zone_id}/snippets/snippet_rules` Updates all snippet rules belonging to the zone. ### Parameters - `params RuleUpdateParams` - `ZoneID param.Field[string]` Path param: Use this field to specify the unique ID of the zone. - `Rules param.Field[[]RuleUpdateParamsRule]` Body param: Lists snippet rules. - `ID string` Specify the unique ID of the rule. - `Expression string` Define the expression that determines which traffic matches the rule. - `LastUpdated Time` Specify the timestamp of when the rule was last modified. - `SnippetName string` Identify the snippet. - `Description string` Provide an informative description of the rule. - `Enabled bool` Indicate whether to execute the rule. ### Returns - `type RuleUpdateResponse interface{…}` Contain the response result. ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" "github.com/cloudflare/cloudflare-go/snippets" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) rule, err := client.Snippets.Rules.Update(context.TODO(), snippets.RuleUpdateParams{ ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"), Rules: cloudflare.F([]snippets.RuleUpdateParamsRule{snippets.RuleUpdateParamsRule{ Expression: cloudflare.F("ip.src eq 1.1.1.1"), SnippetName: cloudflare.F("my_snippet"), }}), }) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", rule) } ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ``` ## Delete zone snippet rules `client.Snippets.Rules.Delete(ctx, body) (*RuleDeleteResponse, error)` **delete** `/zones/{zone_id}/snippets/snippet_rules` Deletes all snippet rules belonging to the zone. ### Parameters - `body RuleDeleteParams` - `ZoneID param.Field[string]` Use this field to specify the unique ID of the zone. ### Returns - `type RuleDeleteResponse interface{…}` Contain the response result. ### Example ```go package main import ( "context" "fmt" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" "github.com/cloudflare/cloudflare-go/snippets" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) rule, err := client.Snippets.Rules.Delete(context.TODO(), snippets.RuleDeleteParams{ ZoneID: cloudflare.F("9f1839b6152d298aca64c4e906b6d074"), }) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", rule) } ``` #### Response ```json { "errors": [ { "message": "something bad happened", "code": 10000 } ], "messages": [ { "message": "something bad happened", "code": 10000 } ], "result": {}, "success": true } ```