# templatize target url with KVM by environment

**URL:** <https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385>\
**Category:** Apigee\
**Tags:** api-runtime\
**Created:** [October 3, 2016, 9:24pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385 "2016-10-03T21:24:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 3, 2016, 9:24pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/1 "2016-10-03T21:24:38Z")

</div>

I would like to templatize the target url, so that the same proxy could be promoted from staging to production, and if the environment configuration KVM was set correctly, the proxy targets would be correct. I could not find the correct syntax for accessing the values of the KVMs.

Here’s what I tried:

```auto
<TargetEndpoint>
   ...
  <HTTPTargetConnection>
    <Properties/>
    <URL>https://{environment}-info-data.google.com/resource</URL>
  </HTTPTargetConnection>
</TargetEndpoint>

```

My KVM was called ‘target\_env’, with a key of ‘environment’ and a value of ‘staging’ or ‘production’.

In staging environment, traffic would hit

[https://staging-info-data.google.com/resource](https://staging-info-data.google.com/resource)

In production environment, traffic would hit:

[https://production-info-data.google.com/resource](https://production-info-data.google.com/resource)

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 3, 2016, 9:38pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/2 "2016-10-03T21:38:06Z")

</div>

Can you please show your KVM Get policy?

You should use something like:

```auto
<KeyValueMapOperations name='KVM-Get-1' mapIdentifier='nameOfMap'>
  <Scope>apiproxy</Scope>
  <Get assignTo='variable_to_set' index='1'>
    <Key>
      <Parameter ref='variable_containing_key'/>
    </Key>
  </Get>
</KeyValueMapOperations>

```

…or…

```auto
<KeyValueMapOperations name='KVM-Get-1' mapIdentifier='nameOfMap'>
  <Scope>apiproxy</Scope>
  <Get assignTo='variable_to_set' index='1'>
    <Key>
      <Parameter>key_name</Parameter>
    </Key>
  </Get>
</KeyValueMapOperations>

```

Which I think, for your case will be:

```auto
<KeyValueMapOperations name='KVM-Get-1' mapIdentifier='target_env'>
  <Scope>apiproxy</Scope>
  <Get assignTo='env_host' index='1'>
    <Key>
      <Parameter ref='env.name'/>
    </Key>
  </Get>
</KeyValueMapOperations><br>

```

And then…

```auto
<TargetEndpoint>
  ...

  <HTTPTargetConnection> 
    <URL>https://{env_host}-info-data.google.com/resource</URL> 
  </HTTPTargetConnection> 
</TargetEndpoint>
<br>

```

---

<div class="post-metadata">

**Author:** ![ssvaidyanathan](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/ssvaidyanathan/32/52045_2.png) [@ssvaidyanathan](https://discuss.google.dev/u/ssvaidyanathan)\
**Post date:** [October 3, 2016, 9:54pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/3 "2016-10-03T21:54:34Z")

</div>

Hi [@jasonbrown](https://community.apigee.com/users/7003/jasonbrown.html)

I think you should use TargetServer for this use case. You can configure TargetServer with the same name for each environment. Use [this](http://docs.apigee.com/management/apis/post/organizations/%7borg_name%7d/environments/%7benv_name%7d/targetservers) API to configure

For staging env, the payload for Target server will be:

```auto
<TargetServer name="target1">
	<Host>staging-info-data.google.com</Host>
    <Port>443</Port>
    <IsEnabled>true</IsEnabled>
    <SSLInfo> 
      <Enabled>true</Enabled> 
    </SSLInfo> 
</TargetServer> 

```

For prod env:

```auto
<TargetServer name="target1">
	<Host>production-info-data.google.com</Host>
    <Port>443</Port>
    <IsEnabled>true</IsEnabled>
    <SSLInfo> 
      <Enabled>true</Enabled> 
    </SSLInfo> 
</TargetServer> 

```

NOTE : Make sure in your Mgmt API URL, you provide the correct environment

```auto
[https://api.enterprise.apigee.com/v1/organizations/{org}/environments/{env}/targetservers](https://api.enterprise.apigee.com/v1/organizations/{org}/environments/{env}/targetservers)

```

You should be able to see them in the Environment Configuration tab on the UI as well once its done.

After the configuration is all set, you can just configure your TargetEndpoint in proxy as

```auto
<HTTPTargetConnection>
	<LoadBalancer>
            <Server name="target1"/>
        </LoadBalancer>
        <Path>/resource</Path>
</HTTPTargetConnection>

```

Depending on which environment you are calling from, it picks the corresponding Target server configuration. With this its more configuration driven as well.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 4, 2016, 5:00pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/4 "2016-10-04T17:00:47Z")

</div>

hi Sai, this worked with one caveat: the request from proxy to target backend is not-secure: “GET http://…” whereas when I hardcode the url, it uses [https://.](https://.). .

does the problem have to do with the sslInfo portion of the payload?

false true false keystore-alias keystore-name truststore-name

If so, where do I find the values for keystore-alias, keystore-name, and truststore-name?

---

<div class="post-metadata">

**Author:** ![ssvaidyanathan](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/ssvaidyanathan/32/52045_2.png) [@ssvaidyanathan](https://discuss.google.dev/u/ssvaidyanathan)\
**Post date:** [October 4, 2016, 5:08pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/5 "2016-10-04T17:08:11Z")

</div>

HI [@jasonbrown](https://community.apigee.com/users/7003/jasonbrown.html) - are you seeing http on the trace tool where you think it should be showing https ? If thats the case, thats a bug in the UI, we have an issue opened for that.

Are you getting the valid response from the target ? If you are, then I believe you are good.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 4, 2016, 5:19pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/6 "2016-10-04T17:19:40Z")

</div>

thank you [@Sai Saran Vaidyanathan](https://community.apigee.com/users/3508/ssvaidyanathan.html). I am getting a valid response from the target. Do you have a link to the bug in a bug-tracker anywhere that I can show my manager?

---

<div class="post-metadata">

**Author:** ![ssvaidyanathan](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/ssvaidyanathan/32/52045_2.png) [@ssvaidyanathan](https://discuss.google.dev/u/ssvaidyanathan)\
**Post date:** [October 4, 2016, 5:30pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/7 "2016-10-04T17:30:20Z")

</div>

HI [@jasonbrown](https://community.apigee.com/users/7003/jasonbrown.html) - unfortunately, its an internal defect tracking system. However the defect ID is APIRT-2947 and once its fixed and released, it will be available in our [release notes](http://docs.apigee.com/release-notes/content/apigee-release-notes). I will track it as well. Will update this post or drop a note once this is released.

If this fixed the issue, please accept the answer so that it is useful for others as well

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 27, 2016, 5:35pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/8 "2016-10-27T17:35:53Z")

</div>

hi [@Sai Saran Vaidyanathan](https://community.apigee.com/users/3508/ssvaidyanathan.html) has a fix to this bug been released? I’m still seeing ‘http’ rather than ‘https’

---

<div class="post-metadata">

**Author:** ![ssvaidyanathan](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/ssvaidyanathan/32/52045_2.png) [@ssvaidyanathan](https://discuss.google.dev/u/ssvaidyanathan)\
**Post date:** [October 27, 2016, 9:42pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/9 "2016-10-27T21:42:59Z")

</div>

hi [@jasonbrown](https://community.apigee.com/users/7003/jasonbrown.html) - its not released yet. Trying to get an ETA

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [January 9, 2017, 3:17pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/10 "2017-01-09T15:17:10Z")

</div>

1st Create - Key Value Operation policy to set keys in API Proxy , as below sample -

Key Vlaue Map Operatoion Policy:

## Set Key with Values:
\<?xml version="1.0" encoding="UTF-8" standalone="yes"?\> SetKeyValue 86400 environment spike\_key 1pm 15pm

2nd - again create - Key Value Operation policy to Get keys in API Proxy , as below sample

## Get Value from Key:
\<?xml version="1.0" encoding="UTF-8" standalone="yes"?\> GetKVM 86400 environment spike\_key

3rd - create Spike Arrest Policy & set tag as below -

## Use Key value in SpikeArrest Policy:
\<?xml version="1.0" encoding="UTF-8" standalone="yes"?\> Spike Arrest-1

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [February 19, 2019, 11:16am UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/11 "2019-02-19T11:16:59Z")

</div>

I followed the same way for mine case like this https://{firstVar}/. But i am getting not able to deploy my proxy it is giving me this error when i am trying to hit my proxy.

```auto
{"fault":{"faultstring":"The Service is temporarily unavailable","detail":{"errorcode":"messaging.adaptors.http.flow.ServiceUnavailable"}}}

```

---

<div class="post-metadata">

**Author:** ![dchiesa1](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/dchiesa1/32/15641_2.png) [@dchiesa1](https://discuss.google.dev/u/dchiesa1)\
**Post date:** [February 19, 2019, 7:13pm UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/12 "2019-02-19T19:13:41Z")

</div>

Is this a question ? If so, please ask a new question. (don’t post new questions in comments to 2 year old questions)

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 14, 2019, 10:36am UTC](https://discuss.google.dev/t/templatize-target-url-with-kvm-by-environment/10385/13 "2019-11-14T10:36:31Z")

</div>

Hi [@Dino](https://community.apigee.com/users/803/dchiesa.html) / [@Sai Saran Vaidyanathan](https://community.apigee.com/users/13148/ssvaidyanathan-1.html)

My OAuth VerifyAccessToken policy looks as shown below

\<?xml version="1.0" encoding="UTF-8" standalone="yes"?\>

\<OAuthV2 async="false"continueOnError="true"enabled="true"name=“OAuthPolicy”\>OAuthPolicy

false

VerifyAccessToken

request.{asekvm.access\_token\_position}.{asekvm.access\_token\_variable}

Policy tries to pick {asekvm.access\_token\_position}, {asekvm.access\_token\_variable} variables from context which are extracted from kvm just before the execution of this policy.

asekvm.access\_token\_position=header

asekvm.access\_token\_variable=access\_token

This policy is resulting in an execution error. Does OAuthV2 policy support templatizing in this way? Or am I doing something wrong?
