API routing via header option and catch all routing

Is it possible to setup API routing via a header parameter?

Let’s assume we have a mapping of the following:

{
  "weather": "http://weather.api.local",
  "shopping-cart": "http://shopping-cart.api.local"
}

Where we can make a call to a tyk configured API endpoint and be able to route the request to the upstream API?

For example:

curl -H "target: weather" http://tyk-instance.com/api/v1/weather" 

Would cause tyk to call http://weather.api.local/api/v1/weather from upstream API service

and

curl -H "target: shopping-cart" http://tyk-instance.com/api/v4/add-item"

Would cause tyk to call http://shopping-cart.api.local/api/v4/add-item from upstream API service

Is it possible to achieve this via multiple defined APIs within tyk, or would I need to approach this problem with a virtual endpoint/custom code?

Finally.

Does tyk allow routing of APIs via the root path without a url segment?

Hi there!

First of all not sure if you task require setting custom header. Tyk allows you to override upstream url using “URL rewrite” plugin, you just need to specify host inside “rewrite_to” field. For example of your “weather” example, it can look like:

"match": "/v1/weather",
"rewrite_to": "http://weather.api.local/api/v1/weather"

In other words you do not need to create multiple APIs to achieve this.

If your use-case require using some conditional logic for routing, like header, it is possible to use our advanced rewriting feature described here URL Rewriting in “Advanced Rewriting” section. Basic idea that you can do conditional url rewrites based on regexp over headers, body, url path, query attributes or user session meta.

Does tyk allow routing of APIs via the root path without a url segment?

Can you clarify it a bit? Do you mean if your versioning strategy use first url segment, tyk should be able to strip version from URL when it get proxies to upstream?

If so, I think it should be done in 2.6 [WIP] feature: strip path version from proxy upstream by asoorm · Pull Request #1449 · TykTechnologies/tyk · GitHub

1 Like

Hey Leon, by this, I meant the ability to setup an API to listen on the root domain, so http://tyk-instance.com/ (<- the / here) will be routed to an API instead of having an API slug. (http://tyk-instance.com/my-api/)

Ah, I see what you mean, for sure it is possible if you will use custom domains, in this case, you need to set “domain” field and set slug to “/”. Also ensure that tyk.conf has enable_custom_domains set to true.

Cheers!

1 Like

Amazing, that solved the routing issue. I will take a look at the advance routing section in the docs.

We are trying to replace our existing gateway with tyk, and would ideally have feature/functional parity for backwards compatibility reasons.

1 Like